Skip to content

Commit

Permalink
fix(login): Updated to support expired tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
foundrium committed Oct 1, 2021
1 parent 54d7bbc commit 56f03a4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
16 changes: 14 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"axios": "^0.21.1",
"jsonwebtoken": "^8.5.1",
"jwks-rsa": "^2.0.4",
"jwt-decode": "^3.1.2",
"secure-ls": "^1.2.6"
},
"devDependencies": {
Expand Down
22 changes: 18 additions & 4 deletions packages/web/src/api/hasteClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { isBrowser } from '../util/environmentCheck';
import createAuth0Client, { Auth0Client } from '@auth0/auth0-spa-js';
import { HasteClientConfiguration } from '../config/hasteClientConfiguration';
import SecureLS from 'secure-ls';
import jwtDecode, { JwtPayload } from 'jwt-decode';

export type HasteAuthentication = {
token: string;
Expand Down Expand Up @@ -68,10 +69,23 @@ export class HasteClient {
const cachedToken = this.ls.get('haste:config');

if (cachedToken) {
return {
token: cachedToken,
isAuthenticated: true,
} as HasteAuthentication;
// eslint-disable-next-line @typescript-eslint/no-unsafe-call
const decoded = jwtDecode<JwtPayload>(cachedToken);
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
const expiration = decoded.exp;

if (expiration && new Date(expiration * 1000) > new Date()) {
return {
token: cachedToken,
isAuthenticated: true,
} as HasteAuthentication;
} else {
this.ls.remove('haste:config');
return {
token: '',
isAuthenticated: false,
} as HasteAuthentication;
}
} else if (idToken) {
this.ls.set('haste:config', idToken);
return {
Expand Down

0 comments on commit 56f03a4

Please sign in to comment.