Skip to content

Commit

Permalink
feat(utils): validate jwt
Browse files Browse the repository at this point in the history
  • Loading branch information
simonhaenisch committed Jun 2, 2020
1 parent 552b640 commit 11902f8
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,16 @@ export const downloadFile = (data: string, mimeType: 'text/csv', fileName: strin
/**
* Parse a Jitbug JWT and return its expiry and user id from the payload.
*/
export const parseJitbugJsonWebToken = (jwt: string): TokenPayload => {
const payload = JSON.parse(window.atob(jwt.split('.')[1].replace(/-/g, '+').replace(/_/g, '/')));
export const parseJitbugJsonWebToken = (jwt: string): TokenPayload | void => {
const [header, payload, signature] = jwt.split('.');

return { expires: payload.exp * 1000, uid: JSON.parse(payload.uid) };
if (!header || !payload || !signature) {
return;
}

const payloadContent = JSON.parse(window.atob(payload.replace(/-/g, '+').replace(/_/g, '/')));

return { expires: payloadContent.exp * 1000, uid: JSON.parse(payloadContent.uid) };
};

export interface TokenPayload {
Expand Down

0 comments on commit 11902f8

Please sign in to comment.