From 3a447f7ab5b281fda361b895f6a3bb44c99a00af Mon Sep 17 00:00:00 2001 From: Piyush Singh Gaur Date: Mon, 9 Feb 2026 15:44:15 +0530 Subject: [PATCH 1/2] fix(authentication-service): ensure JWT lastLogin reflects current login time ensure JWT lastLogin reflects current login time GH-2402 --- .../src/modules/auth/controllers/login.controller.ts | 4 +++- .../src/repositories/user.repository.ts | 4 ++-- .../authentication-service/src/services/idp-login.service.ts | 4 +++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/services/authentication-service/src/modules/auth/controllers/login.controller.ts b/services/authentication-service/src/modules/auth/controllers/login.controller.ts index 44e7be6602..d71f7494ac 100644 --- a/services/authentication-service/src/modules/auth/controllers/login.controller.ts +++ b/services/authentication-service/src/modules/auth/controllers/login.controller.ts @@ -197,7 +197,9 @@ export class LoginController { payload.user?.id && !(await this.userRepo.firstTimeUser(payload.user.id)) ) { - await this.userRepo.updateLastLogin(payload.user.id); + const time = Date.now(); + await this.userRepo.updateLastLogin(payload.user.id, time); + payload.user.lastLogin = new Date(time); } return await this.idpLoginService.createJWT( diff --git a/services/authentication-service/src/repositories/user.repository.ts b/services/authentication-service/src/repositories/user.repository.ts index 2dc153bbe0..98f0c47e7a 100644 --- a/services/authentication-service/src/repositories/user.repository.ts +++ b/services/authentication-service/src/repositories/user.repository.ts @@ -222,11 +222,11 @@ export class UserRepository extends DefaultSoftCrudRepository< return user; } - async updateLastLogin(userId: string): Promise { + async updateLastLogin(userId: string, time?: number): Promise { await super.updateById( userId, { - lastLogin: Date.now(), + lastLogin: time ?? Date.now(), }, { currentUser: {id: userId}, diff --git a/services/authentication-service/src/services/idp-login.service.ts b/services/authentication-service/src/services/idp-login.service.ts index f5df89fd3e..9bf5c98163 100644 --- a/services/authentication-service/src/services/idp-login.service.ts +++ b/services/authentication-service/src/services/idp-login.service.ts @@ -176,7 +176,9 @@ export class IdpLoginService { payload.user?.id && !(await this.userRepo.firstTimeUser(payload.user.id)) ) { - await this.userRepo.updateLastLogin(payload.user.id); + const time = Date.now(); + await this.userRepo.updateLastLogin(payload.user.id, time); + payload.user.lastLogin = new Date(time); } return await this.createJWT(payload, authClient, LoginType.ACCESS); From 30eeedee1b21ba98c99e1e0c504a18d31c01d94e Mon Sep 17 00:00:00 2001 From: Yesha Mavani Date: Fri, 13 Feb 2026 16:26:23 +0530 Subject: [PATCH 2/2] fix(authentication-service): lastlogin date and time returned as a part of response gh-2402 --- .../auth/controllers/login.controller.ts | 9 --------- .../modules/auth/models/token-response.dto.ts | 7 +++++++ .../src/services/idp-login.service.ts | 17 ++++++++--------- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/services/authentication-service/src/modules/auth/controllers/login.controller.ts b/services/authentication-service/src/modules/auth/controllers/login.controller.ts index d71f7494ac..24c25e29c2 100644 --- a/services/authentication-service/src/modules/auth/controllers/login.controller.ts +++ b/services/authentication-service/src/modules/auth/controllers/login.controller.ts @@ -193,15 +193,6 @@ export class LoginController { user: this.user, }; - if ( - payload.user?.id && - !(await this.userRepo.firstTimeUser(payload.user.id)) - ) { - const time = Date.now(); - await this.userRepo.updateLastLogin(payload.user.id, time); - payload.user.lastLogin = new Date(time); - } - return await this.idpLoginService.createJWT( payload, this.client, diff --git a/services/authentication-service/src/modules/auth/models/token-response.dto.ts b/services/authentication-service/src/modules/auth/models/token-response.dto.ts index 324f320220..c4b82e1978 100644 --- a/services/authentication-service/src/modules/auth/models/token-response.dto.ts +++ b/services/authentication-service/src/modules/auth/models/token-response.dto.ts @@ -32,4 +32,11 @@ export class TokenResponse extends CoreModel { type: 'string', }) pubnubToken?: string; + + @property({ + type: 'date', + required: false, + description: 'Last successful interactive login time', + }) + lastLogin?: Date; } diff --git a/services/authentication-service/src/services/idp-login.service.ts b/services/authentication-service/src/services/idp-login.service.ts index 9bf5c98163..9ec1a0c9df 100644 --- a/services/authentication-service/src/services/idp-login.service.ts +++ b/services/authentication-service/src/services/idp-login.service.ts @@ -172,15 +172,6 @@ export class IdpLoginService { throw new HttpErrors.Unauthorized(AuthErrorKeys.UserVerificationFailed); } - if ( - payload.user?.id && - !(await this.userRepo.firstTimeUser(payload.user.id)) - ) { - const time = Date.now(); - await this.userRepo.updateLastLogin(payload.user.id, time); - payload.user.lastLogin = new Date(time); - } - return await this.createJWT(payload, authClient, LoginType.ACCESS); } catch (error) { this.logger.error(error); @@ -336,6 +327,13 @@ export class IdpLoginService { if (this.userActivity?.markUserActivity) this.markUserActivity({...data}, user, userTenant, loginType); + let lastLogin; + if (loginType === LoginType.ACCESS) { + lastLogin = Date.now(); + await this.userRepo.updateLastLogin(user.id ?? '', lastLogin); + } else { + lastLogin = (await this.userRepo.findById(user.id ?? ''))?.lastLogin; + } return new TokenResponse({ accessToken, refreshToken, @@ -343,6 +341,7 @@ export class IdpLoginService { .add(authClient.accessTokenExpiration, 's') .toDate() .getTime(), + lastLogin: new Date(lastLogin ?? 0), }); } catch (error) { this.logger.error(error);