Web Worker Sign Out and Use Go API (#13)

* Calculate checksum and send with file upload

* Limit file upload and display rejected file error

* Add sign in timeout

* Check auth token structure before setting
Clean up

* Use web worker timer to sign out
Remove checksum
Point to Go ota update

* Remove checksum dependency
This commit is contained in:
John Wu
2021-02-08 15:23:36 -08:00
committed by GitHub
parent 1ddf9fe813
commit e1f0006d5e
13 changed files with 1630 additions and 2173 deletions

View File

@@ -1,5 +1,6 @@
import React, { useContext, useEffect, useState } from 'react';
import auth from '../../services/auth';
import React, { useContext, useEffect, useState } from "react";
import auth from "../../services/auth";
import getTimerWorker from "../../services/timer";
const UserContext = React.createContext();
@@ -7,24 +8,46 @@ export const UserProvider = ({ children }) => {
const [fetching, setFetching] = useState(false);
const [token, setToken] = useState(null);
const [error, setError] = useState(null);
let timer;
useEffect(() => {
if (!localStorage) return;
const token = JSON.parse(localStorage.getItem("token"));
if (!token) return;
const { idToken: { jwtToken }} = token;
const verifyToken = async (accessToken) => {
const result = await auth.verify(accessToken);
if (result.authenticated) {
setToken(token);
} else {
await signOut();
}
};
verifyToken(jwtToken);
return () => {};
const t = JSON.parse(localStorage.getItem("token"));
if (!t || !t.idToken || !t.idToken.jwtToken) return;
if (!t.idToken.payload || !t.idToken.payload.exp) return;
setToken(t);
}, []);
useEffect(() => {
if (!token) return;
const { idToken: { jwtToken }} = token;
verifyToken(jwtToken);
return () => {
if (timer) timer.terminate();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [token])
const verifyToken = async (accessToken) => {
const result = await auth.verify(accessToken);
if (!result.authenticated || !token.idToken.payload || !token.idToken.payload.exp) {
signOut();
return;
}
const duration = (1000 * token.idToken.payload.exp) - (new Date()).getTime();
if (!timer) {
timer = getTimerWorker();
timer.onMessage((e) => {
if (e.data === "timeout") {
signOut();
}
})
}
timer.start(duration);
};
const signIn = async (username, password) => {
let result = null;
@@ -92,7 +115,7 @@ export const UserProvider = ({ children }) => {
return result;
};
const signOut = async () => {
const signOut = () => {
setToken(null);
if (!localStorage) return;
localStorage.removeItem("token");