#JWT invalid 'expiresIn' option error

18 messages · Page 1 of 1 (latest)

uneven lantern
#

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)
}
#

app.module:

 JwtModule.register({
      secret: JWTSecret,
      signOptions: { expiresIn: '7d' },
    }),
stoic tide
#

in your app module can you try

JwtModule.register({})

and then in sign Token function

const jwt = this.jwtService.sign({<data>}, {
  secret: this.config.get<string>('JWTSecret'),
  expiresIn: this.config.get<string>('JWTExpiry') | '7d'
});

return jwt;
stoic tide
ornate elbow
stoic tide
dire thicket
uneven lantern
#

exact path: ./auth/auth.controller.ts

dire thicket
#

What about your AuthModule if that exists?

uneven lantern
uneven lantern
# dire thicket What about your `AuthModule` if that exists?
import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthController } from './auth.controller';
import { SteamStrategy } from './steam.strategy';
import { UsersService } from 'src/users/users.service';
import { User } from 'src/users/user.entity';
import { TypeOrmModule } from '@nestjs/typeorm';
import { JwtStrategy } from './jwt.strategy';
import { HttpModule } from '@nestjs/axios';
import { JwtModule } from '@nestjs/jwt';
import { JWTSecret } from 'src/utils/consts';

@Module({
  imports: [
    TypeOrmModule.forFeature([User]),
    HttpModule,
    JwtModule.register({
      secret: JWTSecret,
      signOptions: { expiresIn: 9000000 },
    }),
  ],
  controllers: [AuthController],
  providers: [AuthService, SteamStrategy, UsersService, JwtStrategy],
})
export class AuthModule {}

#

I tried changing the expiresIn

#

with no success

dire thicket
#

Hmm, okay, then AuthController should be working. Can you possibly link to your repository?

uneven lantern
#

how can I hide stuff like api keys? Should I clone it without those files

dire thicket
#

Those should be hidden in a .env file that isn't published to git