Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JWT not expiring in nestjs application even after setting expiresIn value

This is what my auth.module.ts looks like:

import { Module } from "@nestjs/common";
import { ConfigModule, ConfigService } from "@nestjs/config";
import { JwtModule } from "@nestjs/jwt";
import { PassportModule } from "@nestjs/passport";
import { TypeOrmModule } from "@nestjs/typeorm";
import appConfig from "src/config/app.config";
import devConfig from "src/config/dev.config";
import stagConfig from "src/config/stag.config";
import { User } from "src/entities/entity/user.entity";
import { AuthService } from "./auth.service";
import { JwtStrategy } from "./passport-strategies/jwt-strategy";
import { LocalStrategy } from "./passport-strategies/local-strategy";



@Module({
  imports: [
    PassportModule,
    ConfigModule.forRoot({
      load: [appConfig, devConfig, stagConfig],
      ignoreEnvFile: true,
      isGlobal: true,
    }),
    TypeOrmModule.forFeature([
       User
    ]),
    JwtModule.registerAsync({
        imports: [ConfigModule],
        useFactory: async (configService: ConfigService) => ({
          // secret: configService.get<string>('jwt.secret'),
          secret: process.env.TOKEN_KEY,
          signOptions: { expiresIn: 30 }
        }),
        inject: [ConfigService]
      }),
  ],
  providers: [
      AuthService, 
      LocalStrategy, 
      JwtStrategy
  ],
  exports: [AuthService],
})
export class AuthModule {}

As you can see I have set signOptions: { expiresIn: 30 } but when I analyze the token it has no expiration parameter and does not expire.

I am using https://jwt.io/#encoded-jwt to analyze the token:

This is a screenshot of the jwt

like image 351
Rajarshi Ghoshal Avatar asked Oct 31 '25 10:10

Rajarshi Ghoshal


1 Answers

You have to pass the expiresIn value as string and mention s for seconds like this

 JwtModule.register({
      secret: jwtConstants.secret,
      signOptions: { expiresIn: '60s' },
    }),

Do let me know if this works!

like image 114
Afaq Javed Avatar answered Nov 04 '25 02:11

Afaq Javed