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 b04476931b..b2550d15c6 100644 --- a/services/authentication-service/src/modules/auth/controllers/login.controller.ts +++ b/services/authentication-service/src/modules/auth/controllers/login.controller.ts @@ -193,13 +193,6 @@ export class LoginController { user: this.user, }; - if ( - payload.user?.id && - !(await this.userRepo.firstTimeUser(payload.user.id)) - ) { - await this.userRepo.updateLastLogin(payload.user.id); - } - 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/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..9ec1a0c9df 100644 --- a/services/authentication-service/src/services/idp-login.service.ts +++ b/services/authentication-service/src/services/idp-login.service.ts @@ -172,13 +172,6 @@ export class IdpLoginService { throw new HttpErrors.Unauthorized(AuthErrorKeys.UserVerificationFailed); } - if ( - payload.user?.id && - !(await this.userRepo.firstTimeUser(payload.user.id)) - ) { - await this.userRepo.updateLastLogin(payload.user.id); - } - return await this.createJWT(payload, authClient, LoginType.ACCESS); } catch (error) { this.logger.error(error); @@ -334,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, @@ -341,6 +341,7 @@ export class IdpLoginService { .add(authClient.accessTokenExpiration, 's') .toDate() .getTime(), + lastLogin: new Date(lastLogin ?? 0), }); } catch (error) { this.logger.error(error);