old-automod/web/src/utils.ts

30 lines
876 B
TypeScript
Raw Normal View History

2022-01-23 22:23:09 +00:00
import axios from "axios";
import localforage from "localforage";
import { API_URL } from "./App";
2022-01-24 18:01:18 +00:00
async function getAuthHeaders() {
const auth: any = await localforage.getItem('auth');
return {
'x-auth-user': auth.user,
'x-auth-token': auth.token,
}
}
2022-01-23 22:23:09 +00:00
async function getAuth(): Promise<false|{ user: string, token: string }> {
const auth: any = await localforage.getItem('auth');
if (!auth) return false;
try {
const res = await axios.get(API_URL, {
headers: {
'x-auth-user': auth.user,
'x-auth-token': auth.token,
}
});
if (res.data?.authenticated) return { user: auth.user ?? '', token: auth.token ?? '' }
else return false;
} catch(e) { return false } // todo: dont assume we're logged out if death
}
2022-01-24 18:01:18 +00:00
export { getAuth, getAuthHeaders }