needle-innovision / nestjs-tenancy

Multi-tenancy approach for nestjs - currently supported only for mongodb with mongoose
MIT License
186 stars 58 forks source link

Basic/Local Passport problem (Unknown authentication strategy "<strategy>") #9

Closed numnes closed 3 years ago

numnes commented 3 years ago

Hi I’m having an issue implementing a module responsible for the basic auth.

In my project I have the tenant DB with the users collection, in the core module I have a endpoint, /login, that receives the basic login information and returns a JWT.

At first I designed the project storage system keeping the users’ information in a shared database among the tenants, called public database, but eventually I had to change this approach and store the users’ information in his tenant database, but when I changed the mongoose instance from

MongooseModule.forFeature(...)

to

TenancyModule.forFeature(...)

While it was in the mongoose instance it showed no problem, but as soon as I simply change that very line mentioned above I get this error:

[Nest] 8768   - 2021-08-17 4:13:30 PM   [ExceptionsHandler] Unknown authentication strategy "basic" +367357ms
Error: Unknown authentication strategy "basic"
    at attempt (/MY_APP_PATH/api/node_modules/passport/lib/middleware/authenticate.js:190:39)
    at authenticate (/MY_APP_PATH/api/node_modules/passport/lib/middleware/authenticate.js:367:7)
    at Promise (/MY_APP_PATH/api/node_modules/@nestjs/passport/dist/auth.guard.js:91:3)
    at new Promise (<anonymous>)
    at /MY_APP_PATH/api/node_modules/@nestjs/passport/dist/auth.guard.js:83:83
    at MixinAuthGuard.<anonymous> (/MY_APP_PATH/api/node_modules/@nestjs/passport/dist/auth.guard.js:49:36)
    at Generator.next (<anonymous>)
    at fulfilled (/MY_APP_PATH/api/node_modules/@nestjs/passport/dist/auth.guard.js:17:58)
    at process._tickCallback (internal/process/next_tick.js:68:7)

The login route and the basic auth middleware are implemented as follows:

// Login route in the core module.

@UseGuards(AuthGuard("basic"))
@Post("login")
 async login(@Request() { user }) {
   return this.authService.login(user);
 }
// The basic strategy

@Injectable()
export class BasicStrategy extends PassportStrategy(Strategy) {
  constructor(private authService: AuthService) {
    super({ passReqToCallback: true });
  }
  async validate(_, username: string, password: string) {
    const user = await this.authService.validateUser(username, password);
    if (!user) throw new UnauthorizedException();

    return user;
  }
}
// The auth service

@Injectable()
export class AuthService {
  constructor(
    private readonly userService: UsersService,
    private readonly jwtService: JwtService,
  ) {}

  async validateUser(email: string, password: string): Promise<User> {
    const user = await this.userService.find({ email });
    if (user && (await bcrypt.compare(password, user.password))) return user;

    return null;
  }

  async login(user: User) {
    const payload = { email: user.email, sub: user._id };
    return {
      accessToken: this.jwtService.sign(payload),
    };
  }
}
sandeepsuvit commented 3 years ago

@matheusnunesismael I am guessing this has something to do with passport and the way it works when the strategy is Request scoped. Have you checked this error in context of the passport module? You may find similar issues in stack overflow when using request scoped calls. Here is one from the nest issue logs https://github.com/nestjs/nest/issues/4646

sandeepsuvit commented 3 years ago

Closing this because of inactivity or followup.

AdamJessop commented 2 years ago

@matheusnunesismael did you ever get to the bottom of this? I am currently facing the same issue.