Skip to content

Commit

Permalink
jwt access token expiration time
Browse files Browse the repository at this point in the history
  • Loading branch information
mrkeksz committed Oct 25, 2023
1 parent 0d92ef6 commit c8f39ed
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,9 @@ LOGGER_FORMAT=json
# Required: true
# Min length: 128
JWT_SECRET_TOKEN=

# Description: JWT access token expiration time
# Type: string
# Example: "2d", "10h", "60s"
# Default: 30d
JWT_ACCESS_TOKEN_EXPIRES_IN=30d
1 change: 1 addition & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
NODE_ENV=test
JWT_SECRET_TOKEN=VrdWi58a8taiugDqLyefgSC7UkTx32sV9B8JRsv7mf7xtqDGYvyGJQv9Msr6yt9J6CEhCoY4zi8kiSF5xwZybnxUR4NRAHwg6yb3u5vPpSupVPCX4YXBzxBhYf8VFDWL
JWT_ACCESS_TOKEN_EXPIRES_IN=60s
2 changes: 1 addition & 1 deletion src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {JwtAuthGuard} from './guards/jwt-auth.guard'
imports: [ConfigModule],
useFactory: (authConfigService: AuthConfigService) => ({
secret: authConfigService.jwtSecretToken,
signOptions: {expiresIn: '60s'},
signOptions: {expiresIn: authConfigService.jwtSecretTokenExpirationTime},
}),
inject: [ConfigService],
}),
Expand Down
6 changes: 6 additions & 0 deletions src/config/auth/auth.config.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ export const JWT_SECRET_TOKEN = {
minLength: 128,
required: true,
}

export type JwtAccessTokenExpiresInType = string
export const JWT_ACCESS_TOKEN_EXPIRES_IN = {
name: 'JWT_ACCESS_TOKEN_EXPIRES_IN',
default: '30d',
}
3 changes: 2 additions & 1 deletion src/config/auth/auth.config.env-validation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as Joi from 'joi'
import {JWT_SECRET_TOKEN} from './auth.config.constants'
import {JWT_ACCESS_TOKEN_EXPIRES_IN, JWT_SECRET_TOKEN} from './auth.config.constants'

export default {
[JWT_SECRET_TOKEN.name]: Joi.string().required().min(JWT_SECRET_TOKEN.minLength),
[JWT_ACCESS_TOKEN_EXPIRES_IN.name]: Joi.string().default(JWT_ACCESS_TOKEN_EXPIRES_IN.default),
}
11 changes: 10 additions & 1 deletion src/config/auth/auth.config.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import {Injectable} from '@nestjs/common'
import {ConfigService} from '@nestjs/config'
import {JWT_SECRET_TOKEN, JwtSecretTokenType} from './auth.config.constants'
import {
JWT_ACCESS_TOKEN_EXPIRES_IN,
JWT_SECRET_TOKEN,
JwtAccessTokenExpiresInType,
JwtSecretTokenType,
} from './auth.config.constants'

@Injectable()
export class AuthConfigService {
Expand All @@ -9,4 +14,8 @@ export class AuthConfigService {
get jwtSecretToken() {
return this.configService.get(JWT_SECRET_TOKEN.name) as JwtSecretTokenType
}

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

0 comments on commit c8f39ed

Please sign in to comment.