Make use of refresh token#48
Open
maihoangbichtram wants to merge 1 commit intoindustrial-optimization-group:desdeo2from
Open
Make use of refresh token#48maihoangbichtram wants to merge 1 commit intoindustrial-optimization-group:desdeo2from
maihoangbichtram wants to merge 1 commit intoindustrial-optimization-group:desdeo2from
Conversation
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 no setup to check when the access token is expired, neither when and how to send refresh request to backend. When the client is reloaded/refreshed, the refresh token becomes null as it's saved only in the client's own current session, not in persistent state like cookies or local storage.
Fixes:
jsonwebtokento decode tokensfetchandaxiosto refresh access token and then re-run the request when 401 error happensIn src/lib/api.ts
src/lib/server/utils.ts: utils to handle tokens
src/routes/(app)/saved_problems/+page.svelte: Content of the page depends on the value of the store
allProblemssrc/routes/api/logout/+server.ts: Server api
api/logoutto simply remove auth data (tokens)src/routes/api/refresh/+server.ts: Server api
api/refreshsendsrefreshrequest to backend asking for new access tokensrc/routes/api/signIn/+server.ts Server api
api/signInsends request to backend to authenticate the user and ask for tokens. This server api is for setting refresh token as cookie with the same domain as the client side. (Cookies with different domains are removed after refreshing the page)With all fixes above, when the user tries to access the page and error 401 occurs, server api
api/refreshis called. If the refresh token process doesn't succeed, the user will be redirected to/login, so content of src/routes/(app)/+layout.ts is absolete