[ ] Regression
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
I just updated to v6.0.4 and I'm using authguard with graphql & passport-jwt. However, I'm getting Error: Unknown authentication strategy "jwt"
// gql-auth.guard.ts
import { Injectable } from '@nestjs/common';
import { GqlExecutionContext } from '@nestjs/graphql';
import { AuthGuard } from '@nestjs/passport';
@Injectable()
export class GqlAuthGuard extends AuthGuard('jwt') {
getRequest(context: GqlExecutionContext) {
const ctx = GqlExecutionContext.create(context);
return ctx.getContext().req;
}
}
//jwt.strategy.ts
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { ExtractJwt, Strategy, VerifiedCallback } from 'passport-jwt';
import { UserService} from 'src/user/user.service';
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(private userService: UserService) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKey: process.env.JWT_SECRET,
});
}
async validate(payload: any, done: VerifiedCallback) {
const user = await this.userService.findOneByPayload(payload);
if (!user) {
return done(new UnauthorizedException(), false);
}
return done(null, { id: user.id }, payload.iat);
}
}
// auth.module.ts
import { Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';
import { UserModule } from 'src/user/user.module';
import { AuthResolver } from './auth.resolver';
@Module({
imports: [
PassportModule.register({ defaultStrategy: 'jwt' }),
JwtModule.register({ secretOrPrivateKey: process.env.JWT_SECRET }),
UserModule,
],
providers: [AuthResolver],
})
export class AuthModule {}
Nest version: 6.0.4
For Tooling issues:
- Node version: 10.15
- Platform: Windows
Others:
I forgot to import Strategy into Provider :D
please close it thanks
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
I forgot to import Strategy into Provider :D
please close it thanks