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

fix: automatically clear auth token if refresh token is invalid #6760

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

vvagaytsev
Copy link
Collaborator

What this PR does / why we need it:

Which issue(s) this PR fixes:

Fixes #

Special notes for your reviewer:

@@ -86,8 +87,15 @@ export async function refreshAuthTokenAndWriteToConfigStore(
}

log.debug({ msg: `Failed to refresh the token.` })
if (err.message.toLowerCase().includes("invalid refresh token")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we also determine the token validity using the response status code?

Suggested change
if (err.message.toLowerCase().includes("invalid refresh token")) {
if (err.statusCode >= 400 && err.statusCode < 500) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should even test for 401 explicitly

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we ever migrate to other kinds of tokens that need to be decoded, e.g. JWT tokens then we should also delete the token on 400 response though, so I think 400 <= code < 500 is fine

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Backend V2 did not return any status code for invalid token.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to change the backend in that case, to make sure that it will emit correct status code in this case then. Maybe @hph can support you on this?

Copy link
Contributor

@hph hph Jan 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error returned is UNAUTHORIZED, which should map to 401. This status code is available in the httpStatus property, at least when using the frontend client, but I think it should be the same in core. Reference (see error shape above the linked table)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I double-check that and did not see the status code in the TRPCClientError instance. The data property (that should contain httpStatus) was undefined, I'm not sure why.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found the original error code deep inside the error object.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it maybe helpful to infer the specific error shape for the router? See also https://trpc.io/docs/client/vanilla/infer-types#infer-trpcclienterror-types

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it in feaa8d2 - we need the nested cause there. Not sure why the error is wrapped in such a way,

@vvagaytsev vvagaytsev requested a review from stefreak January 14, 2025 12:36
@vvagaytsev vvagaytsev marked this pull request as draft January 14, 2025 13:38
auto-merge was automatically disabled January 14, 2025 13:38

Pull request was converted to draft

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.

3 participants