11import { Inject , Injectable , Logger } from '@nestjs/common' ;
22import { JwtService } from '@nestjs/jwt' ;
33import axios from 'axios' ;
4- import type { Request , Response } from 'express' ;
4+ import type { CookieOptions , Request , Response } from 'express' ;
5+ import ms from 'ms' ;
56
67import { CreateUser } from '@nbw/database' ;
78import type { UserDocument } from '@nbw/database' ;
@@ -22,18 +23,18 @@ export class AuthService {
2223 @Inject ( JwtService )
2324 private readonly jwtService : JwtService ,
2425 @Inject ( 'COOKIE_EXPIRES_IN' )
25- private readonly COOKIE_EXPIRES_IN : string ,
26+ private readonly COOKIE_EXPIRES_IN : ms . StringValue ,
2627 @Inject ( 'FRONTEND_URL' )
2728 private readonly FRONTEND_URL : string ,
2829
2930 @Inject ( 'JWT_SECRET' )
3031 private readonly JWT_SECRET : string ,
3132 @Inject ( 'JWT_EXPIRES_IN' )
32- private readonly JWT_EXPIRES_IN : string ,
33+ private readonly JWT_EXPIRES_IN : ms . StringValue ,
3334 @Inject ( 'JWT_REFRESH_SECRET' )
3435 private readonly JWT_REFRESH_SECRET : string ,
3536 @Inject ( 'JWT_REFRESH_EXPIRES_IN' )
36- private readonly JWT_REFRESH_EXPIRES_IN : string ,
37+ private readonly JWT_REFRESH_EXPIRES_IN : ms . StringValue ,
3738 @Inject ( 'APP_DOMAIN' )
3839 private readonly APP_DOMAIN ?: string ,
3940 ) { }
@@ -171,11 +172,11 @@ export class AuthService {
171172
172173 public async createJwtPayload ( payload : TokenPayload ) : Promise < Tokens > {
173174 const [ accessToken , refreshToken ] = await Promise . all ( [
174- this . jwtService . signAsync ( payload , {
175+ this . jwtService . signAsync < TokenPayload > ( payload , {
175176 secret : this . JWT_SECRET ,
176177 expiresIn : this . JWT_EXPIRES_IN ,
177178 } ) ,
178- this . jwtService . signAsync ( payload , {
179+ this . jwtService . signAsync < TokenPayload > ( payload , {
179180 secret : this . JWT_REFRESH_SECRET ,
180181 expiresIn : this . JWT_REFRESH_EXPIRES_IN ,
181182 } ) ,
@@ -189,7 +190,7 @@ export class AuthService {
189190
190191 private async GenTokenRedirect (
191192 user_registered : UserDocument ,
192- res : Response < any , Record < string , any > > ,
193+ res : Response < unknown , Record < string , unknown > > ,
193194 ) : Promise < void > {
194195 const token = await this . createJwtPayload ( {
195196 id : user_registered . _id . toString ( ) ,
@@ -198,18 +199,16 @@ export class AuthService {
198199 } ) ;
199200
200201 const frontEndURL = this . FRONTEND_URL ;
201- const domain = this . APP_DOMAIN ;
202- const maxAge = parseInt ( this . COOKIE_EXPIRES_IN ) * 1000 ;
202+ const maxAge = ms ( this . COOKIE_EXPIRES_IN ) * 1000 ;
203203
204- res . cookie ( 'token' , token . access_token , {
205- domain : domain ,
204+ const cookieOptions : CookieOptions = {
206205 maxAge : maxAge ,
207- } ) ;
206+ domain : this . APP_DOMAIN ,
207+ path : '/' ,
208+ } ;
208209
209- res . cookie ( 'refresh_token' , token . refresh_token , {
210- domain : domain ,
211- maxAge : maxAge ,
212- } ) ;
210+ res . cookie ( 'token' , token . access_token , cookieOptions ) ;
211+ res . cookie ( 'refresh_token' , token . refresh_token , cookieOptions ) ;
213212
214213 res . redirect ( frontEndURL + '/' ) ;
215214 }
0 commit comments