This commit is contained in:
Elijah Lucian
2021-03-16 20:15:33 -06:00
parent 90b00b4736
commit 8a9604966c
35 changed files with 29279 additions and 0 deletions

45
frontend/src/utils/jwt.ts Normal file
View File

@@ -0,0 +1,45 @@
import { namespace } from '../config'
export type JWT = {
id: string
token: string
exp: number
}
const getJWT = (): null | JWT => {
const id = window.localStorage.getItem(`${namespace}-id`)
const token = window.localStorage.getItem(`${namespace}-token`)
const exp = parseInt(window.localStorage.getItem(`${namespace}-exp`) || '')
if (!id || !token || exp < +new Date()) {
wipeJWT()
return null
}
return {
id,
token,
exp,
}
}
const setJWT = (token: JWT): void => {
window.localStorage.setItem(`${namespace}-id`, token.id)
window.localStorage.setItem(`${namespace}-token`, token.token)
window.localStorage.setItem(`${namespace}-exp`, token.exp + '')
}
const wipeJWT = (): void => {
window.localStorage.removeItem(`${namespace}-id`)
window.localStorage.removeItem(`${namespace}-token`)
window.localStorage.removeItem(`${namespace}-exp`)
}
const setHeaders = () => {
const token = window.localStorage.getItem(`${namespace}-token`)
if (!token) throw new Error('Token gone!')
return { headers: { Authorization: token } }
}
export { getJWT, setJWT, wipeJWT, setHeaders }