mirror of https://github.com/hykilpikonna/AquaDX
[F] Fix: Automatically redirect users if not logged in
parent
9ee3e973c1
commit
f2574b516e
|
@ -78,7 +78,15 @@ export async function post(endpoint: string, params: any, init?: RequestInitWith
|
||||||
if (!res.ok) {
|
if (!res.ok) {
|
||||||
const text = await res.text()
|
const text = await res.text()
|
||||||
console.error(`${res.status}: ${text}`)
|
console.error(`${res.status}: ${text}`)
|
||||||
throw new Error(`${text}`)
|
|
||||||
|
// Try to parse as json
|
||||||
|
let json
|
||||||
|
try {
|
||||||
|
json = JSON.parse(text)
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error(text)
|
||||||
|
}
|
||||||
|
if (json.error) throw new Error(json.error)
|
||||||
}
|
}
|
||||||
|
|
||||||
const ret = res.json()
|
const ret = res.json()
|
||||||
|
@ -102,16 +110,22 @@ async function login(user: { email: string, password: string, turnstile: string
|
||||||
localStorage.setItem('token', data.token)
|
localStorage.setItem('token', data.token)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const isLoggedIn = () => !!localStorage.getItem('token')
|
||||||
|
const ensureLoggedIn = () => !isLoggedIn() && (window.location.href = '/')
|
||||||
|
|
||||||
export const USER = {
|
export const USER = {
|
||||||
register,
|
register,
|
||||||
login,
|
login,
|
||||||
confirmEmail: (token: string) =>
|
confirmEmail: (token: string) =>
|
||||||
post('/api/v2/user/confirm-email', { token }),
|
post('/api/v2/user/confirm-email', { token }),
|
||||||
me: (): Promise<UserMe> =>
|
me: (): Promise<UserMe> => {
|
||||||
post('/api/v2/user/me', {}),
|
ensureLoggedIn()
|
||||||
isLoggedIn: () => !!localStorage.getItem('token'),
|
return post('/api/v2/user/me', {})
|
||||||
|
},
|
||||||
keychip: (): Promise<string> =>
|
keychip: (): Promise<string> =>
|
||||||
post('/api/v2/user/keychip', {}).then(it => it.keychip),
|
post('/api/v2/user/keychip', {}).then(it => it.keychip),
|
||||||
|
isLoggedIn,
|
||||||
|
ensureLoggedIn,
|
||||||
}
|
}
|
||||||
|
|
||||||
export const CARD = {
|
export const CARD = {
|
||||||
|
|
Loading…
Reference in New Issue