Skip to content

Commit

Permalink
chore(js): add LogtoErrorCode.invalidToken
Browse files Browse the repository at this point in the history
  • Loading branch information
IceHe committed Jan 21, 2022
1 parent 22c435f commit 46c8734
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/js/src/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Normalize<T> = keyof T | Normalize2<T>;

const logtoErrorCodes = Object.freeze({
idToken: {
invalidToken: 'invalid token',
invalidIat: 'invalid issued at time',
},
});
Expand Down
2 changes: 1 addition & 1 deletion packages/js/src/utils/id-token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('decodeIdToken', () => {
});

test('decoding invalid JWT string should throw Error', async () => {
expect(() => decodeIdToken('invalid-JWT')).toThrow('invalid token');
expect(() => decodeIdToken('invalid-JWT')).toThrow(new LogtoError('idToken.invalidToken'));
});

test('decoding valid JWT without issuer should throw StructError', async () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/js/src/utils/id-token.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export type IdTokenClaims = s.Infer<typeof IdTokenClaimsSchema>;
export const decodeIdToken = (token: string): IdTokenClaims => {
const { 1: encodedPayload } = token.split('.');
if (!encodedPayload) {
throw new Error('invalid token');
throw new LogtoError('idToken.invalidToken');
}

const json = UrlSafeBase64.decode(encodedPayload);
Expand Down

0 comments on commit 46c8734

Please sign in to comment.