Skip to content

Commit

Permalink
chore(js): add LogtoErrorCode.idToken.invalidToken
Browse files Browse the repository at this point in the history
  • Loading branch information
IceHe committed Jan 21, 2022
1 parent 954dc6d commit 7a6c107
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/js/src/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const logtoErrorCodes = Object.freeze({
verification: {
invalidIat: 'Invalid issued at time',
},
decode: {
invalidToken: 'Invalid token',
},
},
});

Expand Down
4 changes: 3 additions & 1 deletion packages/js/src/utils/id-token.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,9 @@ 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.decode.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 @@ -35,7 +35,7 @@ export const verifyIdToken = async (
export const decodeIdToken = (token: string): IdTokenClaims => {
const { 1: encodedPayload } = token.split('.');
if (!encodedPayload) {
throw new Error('invalid token');
throw new LogtoError('idToken.decode.invalidToken');
}

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

0 comments on commit 7a6c107

Please sign in to comment.