auth icon indicating copy to clipboard operation
auth copied to clipboard

Cannot specify the connection for auth providers.

Open ranseur92 opened this issue 1 year ago • 10 comments

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

ranseur92 avatar Jan 21 '25 11:01 ranseur92

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

github-actions[bot] avatar Feb 12 '25 01:02 github-actions[bot]

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)
  }
}

ranseur92 avatar Feb 15 '25 01:02 ranseur92

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

github-actions[bot] avatar Mar 09 '25 01:03 github-actions[bot]

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

github-actions[bot] avatar Mar 31 '25 02:03 github-actions[bot]

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

github-actions[bot] avatar Apr 22 '25 02:04 github-actions[bot]

@thetutlage Is there any update/progress on this btw? curious to see how it gets implemented.

ranseur92 avatar May 13 '25 18:05 ranseur92

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

github-actions[bot] avatar Jun 04 '25 02:06 github-actions[bot]

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

github-actions[bot] avatar Jun 09 '25 02:06 github-actions[bot]

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

github-actions[bot] avatar Jul 01 '25 02:07 github-actions[bot]

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

github-actions[bot] avatar Jul 07 '25 02:07 github-actions[bot]