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:
jsonwebtoken
to decode tokensfetch
andaxios
to 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
allProblems
src/routes/api/logout/+server.ts: Server api
api/logout
to simply remove auth data (tokens)src/routes/api/refresh/+server.ts: Server api
api/refresh
sendsrefresh
request to backend asking for new access tokensrc/routes/api/signIn/+server.ts Server api
api/signIn
sends 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/refresh
is 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