Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Catch some potential exceptions. Add atob polyfill.
Browse files Browse the repository at this point in the history
leighmacdonald committed Nov 18, 2023

Verified

This commit was signed with the committer’s verified signature.
leighmacdonald Leigh MacDonald
1 parent d7a6c06 commit 1bedc4c
Showing 3 changed files with 24 additions and 14 deletions.
22 changes: 14 additions & 8 deletions frontend/src/api/auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'core-js/stable/atob';
import { jwtDecode, JwtPayload } from 'jwt-decode';
import { logErr } from '../util/errors';
import { emptyOrNullString } from '../util/types';
import { apiCall, EmptyBody } from './common';

export const refreshKey = 'refresh';
@@ -34,19 +36,23 @@ export const refreshToken = async () => {
};

export const isTokenExpired = (token: string): boolean => {
if (!token || token == '') {
if (emptyOrNullString(token)) {
return true;
}

const claims: JwtPayload = jwtDecode(token);
if (!claims || !claims.exp) {
return true;
}

const expirationTimeInSeconds = claims.exp * 1000;
const now = new Date();

return expirationTimeInSeconds <= now.getTime();
try {
const claims: JwtPayload = jwtDecode(token);
if (!claims || !claims.exp) {
return true;
}

const expirationTimeInSeconds = claims.exp * 1000;
return expirationTimeInSeconds <= now.getTime();
} catch (e) {
return true;
}
};

export const writeAccessToken = (token: string) => {
12 changes: 8 additions & 4 deletions frontend/src/api/common.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { logErr } from '../util/errors';
import { parseDateTime } from '../util/text';
import { emptyOrNullString } from '../util/types';
import {
@@ -126,10 +127,13 @@ export const apiCall = async <
credentials: 'include',
method: method.toUpperCase()
};

const accessToken = await getAccessToken(isRefresh ?? false);

if (accessToken != '') {
let accessToken = '';
try {
accessToken = await getAccessToken(isRefresh ?? false);
} catch (e) {
logErr(e);
}
if (!emptyOrNullString(accessToken)) {
headers['Authorization'] = `Bearer ${accessToken}`;
}

4 changes: 2 additions & 2 deletions frontend/src/component/LazyTable.tsx
Original file line number Diff line number Diff line change
@@ -213,8 +213,8 @@ export const LazyTableHeader = <T,>({
col.sortKey != sortColumn
? 'none'
: order == 'asc'
? 'underline'
: 'overline'
? 'underline'
: 'overline'
}}
variant={'button'}
>

0 comments on commit 1bedc4c

Please sign in to comment.