Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot specify the connection for auth providers. #248

Open
ranseur92 opened this issue Jan 21, 2025 · 2 comments
Open

Cannot specify the connection for auth providers. #248

ranseur92 opened this issue Jan 21, 2025 · 2 comments
Assignees
Labels
Good First Issue Want to contribute? Just filter by this label Type: Enhancement Improving an existing feature

Comments

@ranseur92
Copy link

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

@thetutlage thetutlage self-assigned this Jan 21, 2025
@thetutlage thetutlage added Type: Enhancement Improving an existing feature Good First Issue Want to contribute? Just filter by this label labels Jan 21, 2025
Copy link

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

@ranseur92
Copy link
Author

ranseur92 commented Feb 15, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Good First Issue Want to contribute? Just filter by this label Type: Enhancement Improving an existing feature
Projects
None yet
Development

No branches or pull requests

2 participants