Skip to content
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

Make use of refresh token #48

Open
wants to merge 1 commit into
base: desdeo2
Choose a base branch
from

Conversation

maihoangbichtram
Copy link

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:

  • Add jsonwebtoken to decode tokens
  • Add interceptors for fetch and axios to refresh access token and then re-run the request when 401 error happens

In src/lib/api.ts

  • Function refreshToken to call server API to refresh access token
  • Writable store allProblems stores all problems that the user has access to

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 sends refresh request to backend asking for new access token

src/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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant