-
Notifications
You must be signed in to change notification settings - Fork 36
Check refresh token on startup #1274
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
Merged
dajimenezriv-internxt
merged 8 commits into
feat/2-6-6-release
from
check-refresh-token-on-startup
Feb 11, 2026
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ea889e0
Check refresh token on startup
dajimenezriv-internxt 1cf1aa5
Update launchBackupProcesses.ts
dajimenezriv-internxt 53828e2
Update token-scheduler.test.ts
dajimenezriv-internxt 9bda992
Update handlers.ts
dajimenezriv-internxt 69dfcae
Commit
dajimenezriv-internxt 57e9c37
Update token-scheduler.test.ts
dajimenezriv-internxt 2444568
Commit
dajimenezriv-internxt 551d80a
Update .eslintrc.js
dajimenezriv-internxt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { partialSpyOn } from '@/tests/vitest/utils.helper.test'; | ||
| import { checkIfUserIsLoggedIn } from './handlers'; | ||
| import * as getUser from './service'; | ||
| import { TokenScheduler } from '../token-scheduler/TokenScheduler'; | ||
|
|
||
| describe('handlers', () => { | ||
| const getUserMock = partialSpyOn(getUser, 'getUser'); | ||
| const getMillisecondsToRenewMock = partialSpyOn(TokenScheduler, 'getMillisecondsToRenew'); | ||
|
|
||
| describe('checkUserIsLoggedIn', () => { | ||
| beforeEach(() => { | ||
| getUserMock.mockReturnValue({ needLogout: false }); | ||
| }); | ||
|
|
||
| it('should return false if user does not exist', () => { | ||
| // Given | ||
| getUserMock.mockReturnValue(null); | ||
| // When | ||
| const res = checkIfUserIsLoggedIn(); | ||
| // Then | ||
| expect(res).toBe(false); | ||
| }); | ||
|
|
||
| it('should return false if user needs logout', () => { | ||
| // Given | ||
| getUserMock.mockReturnValue({ needLogout: undefined }); | ||
| // When | ||
| const res = checkIfUserIsLoggedIn(); | ||
| // Then | ||
| expect(res).toBe(false); | ||
| }); | ||
|
|
||
| it('should return false if token is expired', () => { | ||
| // Given | ||
| getMillisecondsToRenewMock.mockReturnValue(-1); | ||
| // When | ||
| const res = checkIfUserIsLoggedIn(); | ||
| // Then | ||
| expect(res).toBe(false); | ||
| }); | ||
|
|
||
| it('should return false if cannot get token', () => { | ||
| // Given | ||
| getMillisecondsToRenewMock.mockReturnValue(null); | ||
| // When | ||
| const res = checkIfUserIsLoggedIn(); | ||
| // Then | ||
| expect(res).toBe(false); | ||
| }); | ||
|
|
||
| it('should return true if token is not expired', () => { | ||
| // Given | ||
| getMillisecondsToRenewMock.mockReturnValue(100); | ||
| // When | ||
| const res = checkIfUserIsLoggedIn(); | ||
| // Then | ||
| expect(res).toBe(true); | ||
| }); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,49 +1,53 @@ | ||
| import { logger } from '@/apps/shared/logger/logger'; | ||
| import jwtDecode, { JwtPayload } from 'jwt-decode'; | ||
| import { refreshToken } from '../auth/refresh-token'; | ||
| import { obtainToken } from '../auth/service'; | ||
| import { obtainToken, updateCredentials } from '../auth/service'; | ||
| import { driveServerWip } from '@/infra/drive-server-wip/drive-server-wip.module'; | ||
|
|
||
| const DAYS_BEFORE = 1; | ||
|
|
||
| export class TokenScheduler { | ||
| timeout: NodeJS.Timeout | undefined; | ||
| static timeout: NodeJS.Timeout | undefined; | ||
|
|
||
| getExpiration() { | ||
| const token = obtainToken(); | ||
| const decoded = jwtDecode<JwtPayload>(token); | ||
| if (!decoded.exp) throw new Error('Token does not have expiration time'); | ||
| return decoded.exp * 1000; | ||
| } | ||
|
|
||
| schedule() { | ||
| static getMillisecondsToRenew() { | ||
| try { | ||
| const expirationDate = this.getExpiration(); | ||
| const renewDate = expirationDate - DAYS_BEFORE * 24 * 60 * 60 * 1000; | ||
| const delayInMs = renewDate - Date.now(); | ||
| const token = obtainToken(); | ||
| const decoded = jwtDecode<JwtPayload>(token); | ||
| if (!decoded.exp) throw new Error('Token does not have expiration time'); | ||
|
|
||
| const expiresAt = decoded.exp * 1000; | ||
| const renewAt = expiresAt - DAYS_BEFORE * 24 * 60 * 60 * 1000; | ||
| const msToRenew = renewAt - Date.now(); | ||
|
|
||
| logger.debug({ | ||
| tag: 'AUTH', | ||
| msg: 'Token renew date', | ||
| expiresAt: new Date(expirationDate), | ||
| renewAt: new Date(renewDate), | ||
| expiresAt: new Date(expiresAt), | ||
| renewAt: new Date(renewAt), | ||
| msToRenew, | ||
| }); | ||
|
|
||
| this.timeout = setTimeout(async () => { | ||
| const isRefreshed = await refreshToken(); | ||
| if (isRefreshed) { | ||
| this.schedule(); | ||
| } | ||
| }, delayInMs); | ||
| return msToRenew; | ||
| } catch (error) { | ||
| logger.error({ | ||
| tag: 'AUTH', | ||
| msg: 'Error scheduling refresh token', | ||
| error, | ||
| }); | ||
| logger.error({ tag: 'AUTH', msg: 'Error getting token', error }); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| stop() { | ||
| static schedule() { | ||
| const msToRenew = this.getMillisecondsToRenew(); | ||
| if (msToRenew === null) return; | ||
|
|
||
| this.timeout = setTimeout(async () => { | ||
| const { data } = await driveServerWip.auth.refresh(); | ||
|
|
||
| if (data) { | ||
| updateCredentials({ newToken: data.newToken }); | ||
| this.schedule(); | ||
| } | ||
| }, msToRenew); | ||
| } | ||
|
|
||
| static stop() { | ||
| clearTimeout(this.timeout); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
If the encryption is not available, wouldn't this break?
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.
On windonws the encryption is always available. (electronjs.org/docs/latest/api/safe-storage#safestorageisencryptionavailable)