-
Notifications
You must be signed in to change notification settings - Fork 21
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
Refresh accessToken when JWT access token has expired #1120
Merged
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9abfc89
Refresh accessToken when JWT access token has expired
mohammadranjbarz 7b1d39e
Fix linter errors
mohammadranjbarz 365531f
Merge branch 'main' into 1007_handle_refresh_token_in_frontend
kristoferlund cfd7da3
Explicitly handle TokenExpiredError
kristoferlund 59d13c7
Redirect to start page instead of error page on auth error
kristoferlund a770088
Improve type handling
kristoferlund e5c03dc
Adjust refresh token handling
kristoferlund 24a148e
Add a new 404 page, separate from error page
kristoferlund d1cbd83
CHANGELOG
kristoferlund 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 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 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 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
3 changes: 3 additions & 0 deletions
3
packages/frontend/src/model/auth/dto/refresh-token-input-dto.ts
This file contains 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,3 @@ | ||
import { components } from 'api-types'; | ||
|
||
export type RefreshTokenInputDto = components['schemas']['GenerateTokenDto']; |
1 change: 1 addition & 0 deletions
1
packages/frontend/src/model/auth/interfaces/token-set.interface.ts
This file contains 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,3 +1,4 @@ | ||
export interface TokenSet { | ||
accessToken: string; | ||
refreshToken: string; | ||
} |
This file contains 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 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,5 +1,6 @@ | ||
import axios, { AxiosError, AxiosInstance } from 'axios'; | ||
import { toast } from 'react-hot-toast'; | ||
import { requestApiRefreshToken } from '@/utils/auth'; | ||
|
||
const isJsonBlob = (data): data is Blob => | ||
data instanceof Blob && data.type === 'application/json'; | ||
|
@@ -9,10 +10,48 @@ const isJsonBlob = (data): data is Blob => | |
* | ||
* @param err | ||
*/ | ||
export const handleErrors = ( | ||
err: AxiosError, | ||
export const handleErrors = async ( | ||
err: AxiosError<{ | ||
code: number; | ||
message: string; | ||
statusCode: number; | ||
}>, | ||
handleErrorsAutomatically = true | ||
): AxiosError => { | ||
): Promise< | ||
AxiosError<{ | ||
code: number; | ||
message: string; | ||
statusCode: number; | ||
}> | ||
> => { | ||
let refreshToken; | ||
const recoilPersist = localStorage.getItem('recoil-persist'); | ||
if (recoilPersist) { | ||
refreshToken = JSON.parse(recoilPersist)?.ActiveTokenSet?.refreshToken; | ||
} | ||
|
||
if ( | ||
refreshToken && | ||
recoilPersist && | ||
err?.response?.status === 401 && | ||
// 1092, 1107 are the error codes for invalid jwt token that defined in backend | ||
(err?.response?.data?.code === 1092 || err?.response?.data?.code === 1107) | ||
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. 1092 means unauthenticated user trying to access authenticated resource. This should not trigger a refresh. |
||
) { | ||
// delete the old token from localStorage to prevent infinite loop | ||
delete recoilPersist['ActiveTokenSet']; | ||
localStorage.setItem(JSON.stringify(recoilPersist), 'recoil-persist'); | ||
try { | ||
await requestApiRefreshToken({ refreshToken }); | ||
toast('Please try again'); | ||
return err; | ||
} catch (error) { | ||
console.log( | ||
'refresh accessToken error', | ||
(error as AxiosError)?.response?.data | ||
); | ||
} | ||
} | ||
|
||
// Handling errors automatically means the error will be displayed to the user with a toast. | ||
// If not handled automatically, the error will just be logged to the console and returned. | ||
if (!handleErrorsAutomatically) { | ||
|
@@ -27,8 +66,8 @@ export const handleErrors = ( | |
const json = JSON.parse(text); | ||
toast.error(json.message); | ||
}); | ||
} else if ((err.response.data as Error).message) { | ||
toast.error((err.response.data as Error).message); | ||
} else if (err.response.data.message) { | ||
toast.error(err.response.data.message); | ||
} else { | ||
toast.error('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.
Use
getRecoil(ActiveTokenSet)
, seeauth.ts
for example.