Answer the question
In order to leave comments, you need to log in
How to fix JWTStrategy in Nest.js?
There is a problem with the JWT strategy. Here is the code:
import { Injectable, UnauthorizedException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy, ExtractJwt } from 'passport-jwt';
import { EnvironmentVariables } from '@/config/env-schema';
import { AuthService, JWTPayload } from '@/api/auth/auth.service';
@Injectable()
export class JWTStrategy extends PassportStrategy(Strategy, 'jwt') {
constructor(
private readonly configService: ConfigService<EnvironmentVariables>,
// private readonly authService: AuthService,
) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: configService.get('APP_ENV') !== 'production',
secretOrKey: configService.get('JWT_SECRET'),
});
}
async validate(payload: JWTPayload) {
console.log('validate(payload):', payload);
// const user = await this.authService.getUserById(payload.id);
// if (user === undefined) {
// throw new UnauthorizedException();
// }
return payload;
}
}
Error: Unknown authentication strategy "jwt"
import { Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { PassportModule } from '@nestjs/passport';
import { TypeOrmModule } from '@nestjs/typeorm';
import { getJWTConfig } from '@/config/jwt.config';
import { JWTStrategy } from '@/common/strategies';
import { UserEntity } from '@/api/user/user.entity';
import { UserService } from '@/api/user/user.service';
import { AuthService } from './auth.service';
import { AuthController } from './auth.controller';
@Module({
imports: [
TypeOrmModule.forFeature([UserEntity]),
ConfigModule,
PassportModule.register({
defaultStrategy: 'jwt',
}),
JwtModule.registerAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: getJWTConfig,
}),
],
providers: [AuthService, JWTStrategy, ConfigService, UserService],
controllers: [AuthController],
exports: [],
})
export class AuthModule {}
Answer the question
In order to leave comments, you need to log in
As it turned out, everything was on the surface))
I use the package nestjs-i18n
.
It was like this:
@Injectable()
export class AuthService {
constructor(
@InjectRepository(UserEntity)
private readonly userRepository: Repository<UserEntity>,
private readonly jwtService: JwtService,
private readonly configService: ConfigService<EnvironmentVariables>,
private readonly userService: UserService,
private readonly i18n: I18nRequestScopeService, // Проблема тут
) {}
@Injectable()
export class AuthService {
constructor(
@InjectRepository(UserEntity)
private readonly userRepository: Repository<UserEntity>,
private readonly jwtService: JwtService,
private readonly configService: ConfigService<EnvironmentVariables>,
private readonly userService: UserService,
private readonly i18n: I18nService, // Меняем на I18nService и всё успешно работает ))
) {}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question