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

OAuth: Fix refresh token flow to log out user #36

Merged
merged 1 commit into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/utils-reference/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ npm install --save @raycast/utils

## Changelog

### v1.16.2

- Fixed the refresh token flow to log out the user instead of throwing an error.

### v1.16.1

- Fixed an issue where `bodyEncoding` wasn't properly used in OAuthService.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@raycast/utils",
"version": "1.16.1",
"version": "1.16.2",
"description": "Set of utilities to streamline building Raycast extensions",
"author": "Raycast Technologies Ltd.",
"homepage": "https://developers.raycast.com/utils-reference",
Expand Down
20 changes: 14 additions & 6 deletions src/oauth/OAuthService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,12 @@ export class OAuthService implements OAuthServiceOptions {
const tokens = await this.refreshTokens({
token: currentTokenSet.refreshToken,
});
await this.client.setTokens(tokens);
return tokens.access_token;

// In the case where the refresh token flows fails, nothing is returned and the authorize function is called again.
if (tokens) {
await this.client.setTokens(tokens);
return tokens.access_token;
}
}
return currentTokenSet.accessToken;
}
Expand Down Expand Up @@ -266,10 +270,14 @@ export class OAuthService implements OAuthServiceOptions {
if (!response.ok) {
const responseText = await response.text();
console.error("refresh tokens error:", responseText);
throw new Error(`Error while refreshing tokens: ${response.status} (${response.statusText})\n${responseText}`);
// If the refresh token is invalid, stop the flow here, log out the user and prompt them to re-authorize.
this.client.description = `${this.client.providerName} needs you to sign-in again. Press ⏎ or click the button below to continue.`;
await this.client.removeTokens();
await this.authorize();
} else {
const tokenResponse = this.tokenRefreshResponseParser(await response.json());
tokenResponse.refresh_token = tokenResponse.refresh_token ?? token;
return tokenResponse;
}
const tokenResponse = this.tokenRefreshResponseParser(await response.json());
tokenResponse.refresh_token = tokenResponse.refresh_token ?? token;
return tokenResponse;
}
}
Loading