-
Notifications
You must be signed in to change notification settings - Fork 78
fix(authentication-service): ensure JWT lastLogin reflects current login time #2410
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -222,11 +222,11 @@ export class UserRepository extends DefaultSoftCrudRepository< | |
| return user; | ||
| } | ||
|
|
||
| async updateLastLogin(userId: string): Promise<void> { | ||
| async updateLastLogin(userId: string, time?: number): Promise<void> { | ||
|
||
| await super.updateById( | ||
| userId, | ||
| { | ||
| lastLogin: Date.now(), | ||
| lastLogin: time ?? Date.now(), | ||
|
||
| }, | ||
| { | ||
| currentUser: {id: userId}, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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,13 +327,21 @@ 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; | ||
| } | ||
|
Comment on lines
+331
to
+336
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whats happening here ?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if its first time login then its the current date and we update that in the DB else if its relogin then we just fetch that from DB |
||
| return new TokenResponse({ | ||
| accessToken, | ||
| refreshToken, | ||
| expires: moment() | ||
| .add(authClient.accessTokenExpiration, 's') | ||
| .toDate() | ||
| .getTime(), | ||
| lastLogin: new Date(lastLogin ?? 0), | ||
| }); | ||
| } catch (error) { | ||
| this.logger.error(error); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why removed ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
handled in the service class