@@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common';
22import { JwtService } from '@nestjs/jwt' ;
33import { ConfigService } from '@nestjs/config' ;
44import type { JwtPayload } from '@shared/types' ;
5+ import type { User } from '@core/modules/user' ;
56
67@Injectable ( )
78export class TokenService {
@@ -10,16 +11,16 @@ export class TokenService {
1011 private readonly cfg : ConfigService ,
1112 ) { }
1213
13- async generateTokens ( user : any , sessionId : string ) {
14+ async generateTokens ( user : User , sessionId : string ) {
1415 const domain = this . cfg . get ( 'DOMAIN' ) ;
16+ const audConstraint = this . cfg . getOrThrow ( 'JWT_AUDIENCE' ) ;
1517
1618 const payload = {
1719 jti : sessionId ,
1820 sub : user . id ,
1921 email : user . email ,
2022 iss : btoa ( domain ) ,
21- aud : btoa ( this . cfg . getOrThrow ( 'JWT_AUDIENCE' ) ) ,
22- role : user . role ,
23+ aud : btoa ( audConstraint ) ,
2324 } ;
2425
2526 const [ access , refresh ] = await Promise . all ( [
@@ -38,10 +39,10 @@ export class TokenService {
3839
3940 async validateToken ( token : string , type : 'access' | 'refresh' ) : Promise < JwtPayload > {
4041 try {
41- const secret =
42- type === 'access'
43- ? this . cfg . get ( 'JWT_ACCESS_SECRET' )
44- : this . cfg . get ( 'JWT_REFRESH_SECRET' ) ;
42+ const accessSecret = this . cfg . get ( 'JWT_ACCESS_SECRET' ) ;
43+ const refreshSecret = this . cfg . get ( 'JWT_REFRESH_SECRET' ) ;
44+
45+ const secret = type === 'access' ? accessSecret : refreshSecret ;
4546
4647 return this . jwtService . verifyAsync ( token , { secret } ) ;
4748 } catch ( e ) {
0 commit comments