Cannot specify the connection for auth providers.
Package version
9.3.1
Describe the bug
I have a multi-tenant application, with a seperate database for each tenant. using dynamic subdomains
I want to add authentication using a model from the tenant. but it seems I can't specify which connection to use, for fetching the user?
For my specific use-case, the connection would depend on the context / subdomain. so would need to be evaluated at route-level, not via config etc.
Reproduction repo
No response
This issue has been marked as stale because it has been inactive for more than 21 days. Please reopen if you still need help on this issue
Hey @thetutlage
I'm not sure if it's worth mentioning, but I've found a workaround that works for my use-case, without using custom auth providers etc.
My approach is to use a provider, to inject an adapter onto the base model, when the app is started which pulls the connection from the httpcomtext as a fallback
What's your thoughts on this approach?
import type { ApplicationService } from '@adonisjs/core/types'
import type { LucidModel, ModelAdapterOptions } from '@adonisjs/lucid/types/model'
import { HttpContext } from '@adonisjs/core/http'
import { Adapter, BaseModel } from '@adonisjs/lucid/orm'
import db from '@adonisjs/lucid/services/db'
class ConnectionAdapter extends Adapter {
public modelConstructorClient(modelConstructor: LucidModel, options?: ModelAdapterOptions) {
if (options && options.client) {
return options.client
}
let connection = (options && options.connection) || modelConstructor.connection || db.config.connection
if (connection === 'tenant') {
const ctx = HttpContext.getOrFail()
connection = ctx?.subdomains.tenant || ctx?.params.tenant
}
return db.connection(connection)
}
}
export default class ConnectionProvider {
constructor(protected app: ApplicationService) {}
start() {
BaseModel.$adapter = new ConnectionAdapter(db)
}
}
This issue has been marked as stale because it has been inactive for more than 21 days. Please reopen if you still need help on this issue
This issue has been marked as stale because it has been inactive for more than 21 days. Please reopen if you still need help on this issue
This issue has been marked as stale because it has been inactive for more than 21 days. Please reopen if you still need help on this issue
@thetutlage Is there any update/progress on this btw? curious to see how it gets implemented.
This issue has been marked as stale because it has been inactive for more than 21 days. Please reopen if you still need help on this issue
This issue has been automatically closed because it has been inactive for more than 4 weeks. Please reopen if you still need help on this issue
This issue has been marked as stale because it has been inactive for more than 21 days. Please reopen if you still need help on this issue
This issue has been automatically closed because it has been inactive for more than 4 weeks. Please reopen if you still need help on this issue