-
Notifications
You must be signed in to change notification settings - Fork 4
fix(cli): stop sanity logout from sending env tokens to the session endpoint #1584
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
Closed
shapirodaniel
wants to merge
2
commits into
stack/mint-and-claim/a1/06-ambient-reminder
from
stack/mint-and-claim/a1/07-logout-env-token
+86
−18
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <!-- auto-generated --> | ||
| --- | ||
| '@sanity/cli': patch | ||
| --- | ||
|
|
||
| fix(cli): stop sanity logout from sending env tokens to the session endpoint |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import { | ||
| exitCodes, | ||
| getCliToken, | ||
| getCliUserConfig, | ||
| getUserConfig, | ||
| SanityCommand, | ||
| setCliUserConfig, | ||
|
|
@@ -15,14 +15,27 @@ export class LogoutCommand extends SanityCommand<typeof LogoutCommand> { | |
| public async run(): Promise<void> { | ||
| await this.parse(LogoutCommand) | ||
|
|
||
| const previousToken = await getCliToken() | ||
| if (!previousToken) { | ||
| this.log('No login credentials found') | ||
| // An env-sourced token (SANITY_AUTH_TOKEN, often loaded from ./.env) is not a login | ||
| // session: there is nothing server-side to end, and clearing local config would not stop | ||
| // the next command from picking the variable up again. Robot tokens from `sanity new` make | ||
| // the session endpoint reject outright — so say what to do instead of calling it. | ||
| const envToken = process.env.SANITY_AUTH_TOKEN?.trim() | ||
| if (envToken) { | ||
| this.warn( | ||
| 'SANITY_AUTH_TOKEN is set in the environment (often via ./.env) — logging out cannot end it. Remove that variable to stop acting as its identity.', | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we improve this error message a bit? |
||
| ) | ||
| } | ||
|
|
||
| // Target the stored login session directly: `getCliToken()` prefers the env token, which is | ||
| // exactly the credential `sanity logout` must not send to the session endpoint. | ||
| const sessionToken = getCliUserConfig('authToken') | ||
| if (!sessionToken) { | ||
| if (!envToken) this.log('No login credentials found') | ||
| return | ||
| } | ||
|
|
||
| try { | ||
| await logout() | ||
| await logout(sessionToken) | ||
|
|
||
| this.clearConfig() | ||
| } catch (error) { | ||
|
|
@@ -33,6 +46,14 @@ export class LogoutCommand extends SanityCommand<typeof LogoutCommand> { | |
| this.clearConfig() | ||
| return | ||
| } | ||
| // API failure bodies can name internal services — surface only the status, keep the | ||
| // local credentials so a retry is possible. | ||
| if (isHttpError(error)) { | ||
| this.error( | ||
| `Failed to logout (HTTP ${error.response.statusCode}). Your local session was kept — try again shortly.`, | ||
| {exit: exitCodes.RUNTIME_ERROR}, | ||
| ) | ||
| } | ||
| const err = error instanceof Error ? error : new Error(`${error}`) | ||
| this.error(`Failed to logout: ${err.message}`, {exit: exitCodes.RUNTIME_ERROR}) | ||
| } | ||
|
|
||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.