Skip to content

Commit 53c8198

Browse files
committed
Auto logout if current credentials are invalid
1 parent 9f75877 commit 53c8198

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

packages/cli-kit/src/private/node/session.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {
1414
InvalidGrantError,
1515
} from './session/exchange.js'
1616
import {allDefaultScopes} from './session/scopes.js'
17-
import {store as secureStore, fetch as secureFetch} from './session/store.js'
17+
import {store as secureStore, fetch as secureFetch, remove as secureRemove} from './session/store.js'
1818

1919
import {ApplicationToken, IdentityToken, Session} from './session/schema.js'
2020
import {validateSession} from './session/validate.js'
@@ -151,18 +151,18 @@ describe('ensureAuthenticated when previous session is invalid', () => {
151151
expect(secureFetch).toHaveBeenCalledOnce()
152152
})
153153

154-
test('throws an error if there is no session and prompting is disabled', async () => {
154+
test('throws an error and logs out if there is no session and prompting is disabled,', async () => {
155155
// Given
156156
vi.mocked(validateSession).mockResolvedValueOnce('needs_full_auth')
157157
vi.mocked(secureFetch).mockResolvedValue(undefined)
158158

159159
// When
160-
// eslint-disable-next-line @typescript-eslint/no-floating-promises
161-
expect(ensureAuthenticated(defaultApplications, process.env, {noPrompt: true})).rejects.toThrow(
160+
await expect(ensureAuthenticated(defaultApplications, process.env, {noPrompt: true})).rejects.toThrow(
162161
`The currently available CLI credentials are invalid.
163162
164163
The CLI is currently unable to prompt for reauthentication.`,
165164
)
165+
expect(secureRemove).toHaveBeenCalled()
166166

167167
// Then
168168
await expect(getLastSeenAuthMethod()).resolves.toEqual('none')

packages/cli-kit/src/private/node/session.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {firstPartyDev, themeToken} from '../../public/node/context/local.js'
1818
import {AbortError, BugError} from '../../public/node/error.js'
1919
import {normalizeStoreFqdn, identityFqdn} from '../../public/node/context/fqdn.js'
2020
import {getIdentityTokenInformation, getPartnersToken} from '../../public/node/environment.js'
21-
import {AdminSession} from '../../public/node/session.js'
21+
import {AdminSession, logout} from '../../public/node/session.js'
2222
import {nonRandomUUID} from '../../public/node/crypto.js'
2323

2424
/**
@@ -197,8 +197,9 @@ ${outputToken.json(applications)}
197197

198198
let newSession = {}
199199

200-
function throwOnNoPrompt() {
200+
async function throwOnNoPrompt() {
201201
if (!noPrompt) return
202+
await logout()
202203
throw new AbortError(
203204
`The currently available CLI credentials are invalid.
204205
@@ -208,7 +209,7 @@ The CLI is currently unable to prompt for reauthentication.`,
208209
}
209210

210211
if (validationResult === 'needs_full_auth') {
211-
throwOnNoPrompt()
212+
await throwOnNoPrompt()
212213
outputDebug(outputContent`Initiating the full authentication flow...`)
213214
newSession = await executeCompleteFlow(applications, fqdn)
214215
} else if (validationResult === 'needs_refresh' || forceRefresh) {
@@ -217,7 +218,7 @@ The CLI is currently unable to prompt for reauthentication.`,
217218
newSession = await refreshTokens(fqdnSession.identity, applications, fqdn)
218219
} catch (error) {
219220
if (error instanceof InvalidGrantError) {
220-
throwOnNoPrompt()
221+
await throwOnNoPrompt()
221222
newSession = await executeCompleteFlow(applications, fqdn)
222223
} else if (error instanceof InvalidRequestError) {
223224
await secureStore.remove()

0 commit comments

Comments
 (0)