hensei-web/utils/userToken.tsx
Justin Edmund bd838305e6
Bug fixes and content updates (#270)
* Fix character hovercard z-index

* Allow unauth users to edit their remixed parties

* Adds ultimate_mastery and renamed master_level to defs

* Add granblue_id to weapon keys

* Add granblue_id to AX skills

* Add granblue_id to data-granblue-id

* Added bugfixes to update notes

* Fix types where ItemSelect is used

These need an empty `granblue_id`
2023-03-17 01:35:49 -07:00

44 lines
1.2 KiB
TypeScript

import axios from 'axios'
import ls, { get, set } from 'local-storage'
import { getCookie } from 'cookies-next'
import type { NextApiRequest, NextApiResponse } from 'next'
export const accountCookie = (
req: NextApiRequest | undefined = undefined,
res: NextApiResponse | undefined = undefined
) => {
const options = req && res ? { req, res } : {}
const cookie = getCookie('account', options)
return cookie ? cookie : undefined
}
export const setHeaders = (
req: NextApiRequest | undefined = undefined,
res: NextApiResponse | undefined = undefined
) => {
const cookie = accountCookie(req, res)
if (cookie) {
const parsed = JSON.parse(cookie as string)
if (parsed.token)
axios.defaults.headers.common['Authorization'] = `Bearer ${parsed.token}`
} else {
delete axios.defaults.headers.common['Authorization']
}
}
export const storeEditKey = (id: string, key: string) => {
ls(id, key)
}
export const setEditKey = (id: string, user?: User) => {
if (!user) {
const edit_key = get<string>(id)
axios.defaults.headers.common['X-Edit-Key'] = edit_key
} else {
unsetEditKey()
}
}
export const unsetEditKey = () => {
delete axios.defaults.headers.common['X-Edit-Key']
}