2022-01-23 23:23:09 +01:00
|
|
|
import axios from "axios";
|
|
|
|
import localforage from "localforage";
|
|
|
|
import { API_URL } from "./App";
|
|
|
|
|
2022-01-24 19:01:18 +01: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 23:23:09 +01: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 19:01:18 +01:00
|
|
|
export { getAuth, getAuthHeaders }
|