One of my microservices deals with user login and registration. Upon making a request to localhost:8080
with the body
{ "username": "test", "password":"test"}
, I receive an authentication token like this: { "token":"asdfsadfasdf..." }
.
I need to utilize this token in another microservice to authenticate the user before sending a request to my endpoint. The code snippet for this scenario looks like:
import { Injectable } from "@nestjs/common";
import { AuthGuard } from "@nestjs/passport";
@Injectable()
export class JwtAuthGuard extends AuthGuard('jwt') {}
@Get()
@UseGuards(JwtAuthGuard)
private async getHello(): Promise<void> {
console.log("Hello world");
}
@Module({
imports: [
JwtModule.register({
secret: 'secretKey',
}),
JwtStrategy,
PassportModule.register({ defaultStrategy: 'jwt' })
],
providers: [AppService],
controllers: [AppController]
})
export class AppModule{}
Upon launching my Get
request, I encountered an error message:
[Nest] 10472 - 04.05.2021, 17:14:00 [ExceptionsHandler] Unknown authentication strategy "jwt" +3677ms
Error: Unknown authentication strategy "jwt"
Regarding the authentication strategy, the following block of code is used:
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
constructor() {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: 'secret',
});
}
}
I'm seeking guidance on what went wrong with setting up my authentication strategy and why it might not be working correctly.
Thank you for any assistance!