Hello, hope you are having a great day!
I have encountered an issue using @nestjs/jwt:
Full error:
[Nest] 14072 - 02/28/2023, 6:12:00 PM ERROR [ExceptionsHandler] invalid expiresIn option for string payload
Error: invalid expiresIn option for string payload
Jwt.strategy:
import { ExtractJwt, Strategy } from 'passport-jwt';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable } from '@nestjs/common';
import { JWTSecret } from '../utils/consts';
import { SteamUserParams } from 'src/utils/types';
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor() {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false,
secretOrKey: JWTSecret,
});
}
async validate(payload: SteamUserParams) {
return { userId: payload.steamId };
}
Controller:
@Get('jwt')
async steamReturn(@Req() req: Request) {
console.log(req.user);
const payload = await this.authService.getUserJwt(req.user);
return {
access_token: this.jwtService.sign(payload),
};
}
Frontend:
async function login() {
window.location.href = 'http://localhost:3001/auth'
const res = await axios.get('http://localhost:3001/auth/jwt')
localStorage.setItem('access_token', res.data.access_token)
}