Reputation: 135
I'm using NestJs as framework and jwt for auth. I tried to login to the endpoint using user credentials, then I receive a jwt token as a response. After that, I use that token (bearer token) to get access to all the users info(code below) but I'm getting the following error:
Error
[Nest] 17189 - 03/15/2021, 12:52:59 PM [ExceptionsHandler] this.validate is not a function +47159ms
TypeError: this.validate is not a function
at JwtStrategy.<anonymous> (/Users/axels18m/Desktop/Work/Testing/nest-twitter-project/node_modules/@nestjs/passport/dist/passport/passport.strategy.js:19:55)
at Generator.next (<anonymous>)
at /Users/axels18m/Desktop/Work/Testing/nest-twitter-project/node_modules/@nestjs/passport/dist/passport/passport.strategy.js:8:71
at new Promise (<anonymous>)
at __awaiter (/Users/axels18m/Desktop/Work/Testing/nest-twitter-project/node_modules/@nestjs/passport/dist/passport/passport.strategy.js:4:12)
at JwtStrategy.callback [as _verify] (/Users/axels18m/Desktop/Work/Testing/nest-twitter-project/node_modules/@nestjs/passport/dist/passport/passport.strategy.js:16:45)
at /Users/axels18m/Desktop/Work/Testing/nest-twitter-project/node_modules/passport-jwt/lib/strategy.js:123:34
at /Users/axels18m/Desktop/Work/Testing/nest-twitter-project/node_modules/jsonwebtoken/verify.js:223:12
at getSecret (/Users/axels18m/Desktop/Work/Testing/nest-twitter-project/node_modules/jsonwebtoken/verify.js:90:14)
at Object.module.exports [as verify] (/Users/axels18m/Desktop/Work/Testing/nest-twitter-project/node_modules/jsonwebtoken/verify.js:94:10)
Here is the endpoint where the error is raised (user.controller.ts)
@Controller('user')
export class UserController
{
constructor(
private userService: UserService
) {}
@hasRoles('admin')
@UseGuards(JwtAuthGuard, RolesGuard)
@Get()
getAll(): Observable<User[]>
{
return this.userService.getAll();
}
}
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy)
{
constructor(private configService: ConfigService)
{
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false,
secretOrKey: configService.get('JWT_SECRET')
});
}
async validator(payload: any)
{
return { 'user': payload.user , 'username': payload.username };
}
}
Steps for getAll users:
@Post('login')
login(@Body() user: User): Observable<Object>
{
return this.userService.login(user).pipe(
map((jwt: string) => { return { access_token: jwt} })
)
}
@UserGuards(AuthGuard('jwt'), RolesGuard)
instead of @UseGuards(JwtAuthGuard, RolesGuard)
Upvotes: 1
Views: 3684
Reputation: 70570
In your JwtStrategy
you have the async
method valdiator
not validate
. This method needs to be validate
in order for @nestjs/passport
to send on the method properly to passport
Upvotes: 4