@@ -449,9 +449,7 @@ export class UserService {
449
449
try {
450
450
this . validateEmail ( email . toLowerCase ( ) ) ;
451
451
const userData = await this . userRepository . checkUserExist ( email . toLowerCase ( ) ) ;
452
-
453
452
const userSessionDetails = await this . userRepository . fetchUserSessions ( userData ?. id ) ;
454
-
455
453
if ( Number ( process . env . SESSIONS_LIMIT ) <= userSessionDetails ?. length ) {
456
454
throw new BadRequestException ( ResponseMessages . user . error . sessionLimitReached ) ;
457
455
}
@@ -475,8 +473,10 @@ export class UserService {
475
473
} else {
476
474
const decryptedPassword = await this . commonService . decryptPassword ( password ) ;
477
475
const tokenDetails = await this . generateToken ( email . toLowerCase ( ) , decryptedPassword , userData ) ;
478
-
476
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
477
+ const decodedToken : any = jwt . decode ( tokenDetails ?. access_token ) ;
479
478
const sessionData = {
479
+ id : decodedToken . sid ,
480
480
sessionToken : tokenDetails ?. access_token ,
481
481
userId : userData ?. id ,
482
482
expires : tokenDetails ?. expires_in ,
@@ -489,10 +489,7 @@ export class UserService {
489
489
let accountData ;
490
490
if ( null === fetchAccountDetails ) {
491
491
accountData = {
492
- sessionToken : tokenDetails ?. access_token ,
493
492
userId : userData ?. id ,
494
- expires : tokenDetails ?. expires_in ,
495
- refreshToken : tokenDetails ?. refresh_token ,
496
493
keycloakUserId : userData ?. keycloakUserId ,
497
494
type : TokenType . BEARER_TOKEN
498
495
} ;
@@ -502,17 +499,8 @@ export class UserService {
502
499
addSessionDetails = await this . userRepository . createSession ( finalSessionData ) ;
503
500
} ) ;
504
501
} else {
505
- accountData = {
506
- sessionToken : tokenDetails ?. access_token ,
507
- userId : userData ?. id ,
508
- expires : tokenDetails ?. expires_in ,
509
- refreshToken : tokenDetails ?. refresh_token
510
- } ;
511
-
512
- await this . userRepository . updateAccountDetails ( accountData ) . then ( async ( response ) => {
513
- const finalSessionData = { ...sessionData , accountId : response . id } ;
514
- addSessionDetails = await this . userRepository . createSession ( finalSessionData ) ;
515
- } ) ;
502
+ const finalSessionData = { ...sessionData , accountId : fetchAccountDetails . id } ;
503
+ addSessionDetails = await this . userRepository . createSession ( finalSessionData ) ;
516
504
}
517
505
518
506
const finalResponse = {
@@ -554,26 +542,18 @@ export class UserService {
554
542
) ;
555
543
this . logger . debug ( `tokenResponse::::${ JSON . stringify ( tokenResponse ) } ` ) ;
556
544
// Fetch the details from account table based on userid and refresh token
557
- const userAccountDetails = await this . userRepository . fetchAccountByRefreshToken (
558
- userByKeycloakId ?. [ 'id' ] ,
559
- refreshToken
560
- ) ;
545
+ const userAccountDetails = await this . userRepository . checkAccountDetails ( userByKeycloakId ?. [ 'id' ] ) ;
561
546
// Update the account details with latest access token, refresh token and exp date
562
547
if ( ! userAccountDetails ) {
563
548
throw new NotFoundException ( ResponseMessages . user . error . userAccountNotFound ) ;
564
549
}
565
- const updateAccountDetails : IUpdateAccountDetails = {
566
- accessToken : tokenResponse . access_token ,
567
- refreshToken : tokenResponse . refresh_token ,
568
- expiresAt : tokenResponse . expires_in ,
569
- accountId : userAccountDetails . id
570
- } ;
571
- const updateAccountDetailsResponse = await this . userRepository . updateAccountDetailsById ( updateAccountDetails ) ;
572
- // Delete the preveious session record and create new one
573
- if ( ! updateAccountDetailsResponse ) {
574
- throw new InternalServerErrorException ( ResponseMessages . user . error . errorInUpdateAccountDetails ) ;
550
+ // Fetch session details
551
+ const sessionDetails = await this . userRepository . fetchSessionByRefreshToken ( refreshToken ) ;
552
+ if ( ! sessionDetails ) {
553
+ throw new NotFoundException ( ResponseMessages . user . error . userSeesionNotFound ) ;
575
554
}
576
- const deletePreviousSession = await this . userRepository . deleteSessionRecordByRefreshToken ( refreshToken ) ;
555
+ // Delete previous session
556
+ const deletePreviousSession = await this . userRepository . deleteSession ( sessionDetails . id ) ;
577
557
if ( ! deletePreviousSession ) {
578
558
throw new InternalServerErrorException ( ResponseMessages . user . error . errorInDeleteSession ) ;
579
559
}
@@ -583,7 +563,7 @@ export class UserService {
583
563
expires : tokenResponse . expires_in ,
584
564
refreshToken : tokenResponse . refresh_token ,
585
565
sessionType : SessionType . USER_SESSION ,
586
- accountId : updateAccountDetailsResponse . id
566
+ accountId : userAccountDetails . id
587
567
} ;
588
568
const addSessionDetails = await this . userRepository . createSession ( sessionData ) ;
589
569
if ( ! addSessionDetails ) {
0 commit comments