switchit-conseil / nestjs-oauth2-server-module

OAuth2 Server for NestJS
MIT License
71 stars 33 forks source link

Support Request scope to UserLoader and UserValidator object #14

Open prakash290 opened 4 years ago

prakash290 commented 4 years ago

Our application is developed with multiple tenant and multiple database. Based on request we determine which database connection should be establish. we can able to achieve this using nest js Request Scope. So the same thing how to achieve for UserLoader and UserValidator.

database : PostgreSQL Client : Client A, Client B, Client C

Each client can have separate database but entity structure is same. I want to validate user based on client type which will come through the request header.

TenancyModule : which provides database connection based on request header.

import { Global, Module, Scope } from '@nestjs/common'; import { REQUEST } from '@nestjs/core'; import { getConnection } from 'typeorm';

const connectionFactory = { provide: 'CONNECTION', scope: Scope.REQUEST, useFactory: (req) => { const tenant = getConnection(req.headers.client); return getConnection(tenant); }, inject: [REQUEST], };

@Global() @Module({ providers: [connectionFactory], exports: ['CONNECTION'], }) export class TenancyModule {}

User Service class:

@Injectable({scope: Scope.REQUEST}) export class UserService { private readonly userRepository: Repository;

constructor(@Inject('CONNECTION') connection) { this.userRepository = connection.getRepository(User); }

I want UserLoader to support request scope so that I can get request information based on this I can able to validate User.