Skip to content

Commit 3c588e9

Browse files
feat: distinguish between missing and invalid state (#1099)
1 parent ccc53a5 commit 3c588e9

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

__tests__/Auth0Client/handleRedirectCallback.test.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
} from '../constants';
3131

3232
import { DEFAULT_AUTH0_CLIENT } from '../../src/constants';
33+
import { GenericError } from '../../src';
3334

3435
jest.mock('es-cookie');
3536
jest.mock('../../src/jwt');
@@ -204,6 +205,9 @@ describe('Auth0Client', () => {
204205

205206
expect(error).toBeDefined();
206207
expect(error.message).toBe('Invalid state');
208+
expect(error.error).toBe('missing_transaction');
209+
expect(error).toBeInstanceOf(Error);
210+
expect(error).toBeInstanceOf(GenericError);
207211
});
208212

209213
it('returns the transactions appState', async () => {
@@ -269,8 +273,9 @@ describe('Auth0Client', () => {
269273

270274
it('should fail with an error if the state in the transaction does not match the request', async () => {
271275
const auth0 = setup();
276+
let error;
272277

273-
await expect(async () => {
278+
try {
274279
await loginWithRedirect(
275280
auth0,
276281
{},
@@ -281,7 +286,15 @@ describe('Auth0Client', () => {
281286
}
282287
}
283288
);
284-
}).rejects.toEqual(new Error('Invalid state'));
289+
} catch (e) {
290+
error = e;
291+
}
292+
293+
expect(error).toBeDefined();
294+
expect(error.message).toBe('Invalid state');
295+
expect(error.error).toBe('state_mismatch');
296+
expect(error).toBeInstanceOf(Error);
297+
expect(error).toBeInstanceOf(GenericError);
285298
});
286299

287300
it('should not validate the state if there is no state in the transaction', async () => {

src/Auth0Client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ export class Auth0Client {
493493
const transaction = this.transactionManager.get();
494494

495495
if (!transaction) {
496-
throw new Error('Invalid state');
496+
throw new GenericError('missing_transaction', 'Invalid state');
497497
}
498498

499499
this.transactionManager.remove();
@@ -512,7 +512,7 @@ export class Auth0Client {
512512
!transaction.code_verifier ||
513513
(transaction.state && transaction.state !== state)
514514
) {
515-
throw new Error('Invalid state');
515+
throw new GenericError('state_mismatch', 'Invalid state');
516516
}
517517

518518
const organizationId = transaction.organizationId;

0 commit comments

Comments
 (0)