diff --git a/backend/src/modules/auth-module/services/token-management.service.ts b/backend/src/modules/auth-module/services/token-management.service.ts index a7136c6..73ad211 100644 --- a/backend/src/modules/auth-module/services/token-management.service.ts +++ b/backend/src/modules/auth-module/services/token-management.service.ts @@ -5,33 +5,54 @@ import { JwtService } from '@nestjs/jwt'; @Injectable() export class TokenManagementService { - - private readonly ACCESS_TOKEN_EXPIRY = '15m'; - private readonly REFRESH_TOKEN_EXPIRY = '7d'; + private readonly ACCESS_TOKEN_EXPIRY: string; + private readonly REFRESH_TOKEN_EXPIRY: string; + private readonly JWT_SECRET_AT: string; + private readonly JWT_SECRET_RT: string; constructor( private readonly jwt: JwtService, private readonly configService: ConfigService - ) {} + ) { + this.ACCESS_TOKEN_EXPIRY = this.configService.get( + 'ACCESS_TOKEN_EXPIRY' + ); + this.REFRESH_TOKEN_EXPIRY = this.configService.get( + 'REFRESH_TOKEN_EXPIRY' + ); + this.JWT_SECRET_AT = this.configService.get('JWT_SECRET_AT'); + this.JWT_SECRET_RT = this.configService.get('JWT_SECRET_RT'); + } public async generateTokens(userId: number, email: string): Promise { - const [access_token, refresh_token] = await Promise.all([ - this.jwt.signAsync( - { sub: userId, email }, - { - expiresIn: this.ACCESS_TOKEN_EXPIRY, - secret: this.configService.get('JWT_SECRET_AT'), - } - ), - this.jwt.signAsync( - { sub: userId, email }, - { - expiresIn: this.REFRESH_TOKEN_EXPIRY, - secret: this.configService.get('JWT_SECRET_RT'), - } - ), - ]); - + const access_token: string = await this.createAccessToken(userId, email); + const refresh_token: string = await this.createRefreshToken(userId, email); return { access_token, refresh_token }; } + + private async createAccessToken( + userId: number, + email: string + ): Promise { + return this.jwt.signAsync( + { sub: userId, email }, + { + expiresIn: this.ACCESS_TOKEN_EXPIRY, + secret: this.JWT_SECRET_AT, + } + ); + } + + private async createRefreshToken( + userId: number, + email: string + ): Promise { + return this.jwt.signAsync( + { sub: userId, email }, + { + expiresIn: this.REFRESH_TOKEN_EXPIRY, + secret: this.JWT_SECRET_RT, + } + ); + } } diff --git a/backend/src/modules/auth-module/strategies/access-token.strategie.ts b/backend/src/modules/auth-module/strategies/access-token.strategie.ts index 250b162..00dea31 100644 --- a/backend/src/modules/auth-module/strategies/access-token.strategie.ts +++ b/backend/src/modules/auth-module/strategies/access-token.strategie.ts @@ -1,7 +1,7 @@ +import { Injectable } from '@nestjs/common'; import { PassportStrategy } from '@nestjs/passport'; import { Strategy, ExtractJwt } from 'passport-jwt'; import { ConfigService } from '@nestjs/config'; -import { Injectable } from '@nestjs/common'; import { JwtPayload } from '../models/types'; @Injectable() @@ -10,13 +10,17 @@ export class AccessTokenStrategy extends PassportStrategy( 'jwt-access-token' ) { constructor(private readonly configService: ConfigService) { - super({ - jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), - secretOrKey: configService.get('JWT_SECRET_AT'), - }); + super(AccessTokenStrategy.getJwtConfig(configService)); } - public async validate(payload: JwtPayload) { + private static getJwtConfig(configService: ConfigService): any { + return { + jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), + secretOrKey: configService.get('JWT_SECRET_AT'), + }; + } + + public async validate(payload: JwtPayload): Promise { return payload; } } diff --git a/backend/src/modules/auth-module/strategies/refresh-token.strategie.ts b/backend/src/modules/auth-module/strategies/refresh-token.strategie.ts index dc7ee43..4d6701b 100644 --- a/backend/src/modules/auth-module/strategies/refresh-token.strategie.ts +++ b/backend/src/modules/auth-module/strategies/refresh-token.strategie.ts @@ -1,7 +1,7 @@ +import { Injectable, ForbiddenException } from '@nestjs/common'; import { PassportStrategy } from '@nestjs/passport'; import { Strategy, ExtractJwt } from 'passport-jwt'; import { ConfigService } from '@nestjs/config'; -import { ForbiddenException, Injectable } from '@nestjs/common'; import { Request } from 'express'; @Injectable() @@ -10,11 +10,15 @@ export class RefreshTokenStrategy extends PassportStrategy( 'jwt-refresh-token' ) { constructor(private readonly configService: ConfigService) { - super({ + super(RefreshTokenStrategy.createJwtStrategyOptions(configService)); + } + + private static createJwtStrategyOptions(configService: ConfigService): any { + return { jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), - secretOrKey: configService.get('JWT_SECRET_RT'), + secretOrKey: configService.get('JWT_SECRET_RT'), passReqToCallback: true, - }); + }; } public async validate(req: Request, payload: any) {