-
Notifications
You must be signed in to change notification settings - Fork 84
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
Failed refresh attempt should remove access token cookie #790
Comments
Another unexpected behaviour observed by @michael-pxr is that the |
Another way of getting to this state is if the dev changes the apiBasePath. This would cause issues cause the refresh API path would change, which in turn would cause the existing refresh token that the client has not to be sent to the new refresh API path, causing it to send a 401 which would put the user in a stuck state |
This has been fixed in the latest versions of our backend SDKs. |
Hi,
I have a NextJS application that calls an API (a NodeJS server) for its data. The API implements the server-side SuperTokens SDK. The NextJS application only implements the client-side SDK. I.e. only the API communicates with the supertokens-core.
When the NextJS app does a server-side request to a protected API endpoint, it is possible that the access token has expired. If so, the NextJS app must communicate this to the client, such that the client can try to refresh the access token.
The NextJS server-side code communicates the 'try refresh token' message back to the client:
If the client needs to refresh, it calls
Session.attemptRefreshingSession
and reloads the page on success. Note that if the refresh attempt fails, it does not redirect the user to the login page. Instead, the page will render different content if the user is logged in or not.Currently, the NextJS server tell the client to attempt a refresh if the API request returns a 401. However, there are different reasons why the API might return a 401. E.g.
Ideally, the NextJS server should make the distinction between these two cases by checking if the
sAccessToken
cookie is set. However, thesAccessToken
cookie is still set when the refresh token has expired. I've tested this as follows:example.com/auth
.sAccessToken
cookie is set. The cookie will expire next year. The JWT token expires in 5 seconds.sRefreshToken
cookie is set. It will expire in 1 minute.FrontToken
header is set.example.com/my-info
). This fails with status 401 and the client is told to attempt a refresh.api.example.com/auth/session/refresh
with cookiessAccessToken
st-last-access-token-update
sFrontToken
{"message":"unauthorised"}
. There are noFront-Token
orSet-Cookie
headers.sAccessToken
cookie is still set.In a Discord support-questions thread I was told that the expected behaviour is that the
sAccessToken
should be removed when a refresh attempt fails. However, this is not the case.Since the
sAccessToken
cookie is still set, the NextJS server cannot use this cookie to determine whether a refresh attempt should be made or not. (As a work around, the NextJS server can check to see if thesFrontToken
cookie exists or not.)In conclusion: If an attempt to refresh an access token fails, the access token is not removed. This means the server application cannot use the existence of the
sAccessToken
cookie to determine whether or not the client should attempt a refresh. To fix this, a failed attempt to refresh an access token must remove the access token.The text was updated successfully, but these errors were encountered: