Skip to content

Commit

Permalink
jwt fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkeksz committed Oct 25, 2023
1 parent 80ec91c commit 15fe78c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
19 changes: 15 additions & 4 deletions src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import {JwtModule} from '@nestjs/jwt'
import {JwtStrategy} from './strategies/jwt.strategy'
import {APP_GUARD} from '@nestjs/core'
import {JwtAuthGuard} from './guards/jwt-auth.guard'
import {
JWT_ACCESS_TOKEN_EXPIRES_IN,
JWT_SECRET_TOKEN,
JwtAccessTokenExpiresInType,
JwtSecretTokenType,
} from '../config/auth/auth.config.constants'

@Module({
imports: [
Expand All @@ -18,10 +24,15 @@ import {JwtAuthGuard} from './guards/jwt-auth.guard'
JwtModule.registerAsync({
global: true,
imports: [ConfigModule],
useFactory: (authConfigService: AuthConfigService) => ({
secret: authConfigService.jwtSecretToken,
signOptions: {expiresIn: authConfigService.jwtSecretTokenExpirationTime},
}),
useFactory: (configService: ConfigService) => {
const secret = configService.get(JWT_SECRET_TOKEN.name) as JwtSecretTokenType
const expiresIn = configService.get(
JWT_ACCESS_TOKEN_EXPIRES_IN.name,
) as JwtAccessTokenExpiresInType

if (!secret || !expiresIn) throw new Error('JWT config is not defined')
return {secret, signOptions: {expiresIn}}
},
inject: [ConfigService],
}),
],
Expand Down
6 changes: 1 addition & 5 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {Injectable} from '@nestjs/common'
import {UsersService} from '../users/users.service'
import {JwtService} from '@nestjs/jwt'
import {AuthConfigService} from '../config/auth/auth.config.service'
import {User} from '../users/entities/user.entity'
import {Payload} from './entities/payload.entity'

Expand All @@ -10,14 +9,11 @@ export class AuthService {
constructor(
private usersService: UsersService,
private jwtService: JwtService,
private authConfigService: AuthConfigService,
) {}

login(userID: User['id']) {
const payload: Omit<Omit<Payload, 'iat'>, 'exp'> = {sub: userID}
return {
access_token: this.jwtService.sign(payload, {secret: this.authConfigService.jwtSecretToken}),
}
return {access_token: this.jwtService.sign(payload)}
}

async validateUser(email: string, password: string): Promise<{id: User['id']} | null> {
Expand Down
2 changes: 1 addition & 1 deletion src/config/auth/auth.config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class AuthConfigService {
return this.configService.get(JWT_SECRET_TOKEN.name) as JwtSecretTokenType
}

get jwtSecretTokenExpirationTime() {
get jwtAccessTokenExpiresIn() {
return this.configService.get(JWT_ACCESS_TOKEN_EXPIRES_IN.name) as JwtAccessTokenExpiresInType
}
}

0 comments on commit 15fe78c

Please sign in to comment.