fix auth bug [FE]

This commit is contained in:
riqo 2021-02-27 13:42:53 -06:00
commit ba2826484f

View file

@ -14,16 +14,23 @@ const state = reactive<AuthState>({
const AUTH_KEY = 'crimata_token'; const AUTH_KEY = 'crimata_token';
const token = window.localStorage.getItem(AUTH_KEY); const token = window.localStorage.getItem(AUTH_KEY);
if (token) {
state.accessToken = token;
window.localStorage.setItem(AUTH_KEY, token);
}
const authToken = async () => { const authToken = async () => {
const { invoke } = useIpc(); const { invoke } = useIpc();
try { try {
state.accessToken = await invoke('auth-session', token); const res = await invoke('auth-session', token);
window.localStorage.setItem(AUTH_KEY, res);
state.accessToken = res;
} }
catch(e) { catch(e) {
state.error = e; state.error = e;
console.log('ERROR: failed authentication'); console.log('ERROR: failed authentication');
window.localStorage.removeItem(AUTH_KEY); window.localStorage.removeItem(AUTH_KEY);
state.accessToken = null;
} }
} }
@ -32,10 +39,6 @@ window.ipcRenderer.on("auth", async (_event, _arg) => {
await authToken(); await authToken();
}); });
if (token) {
state.accessToken = token;
}
export const useAuth = () => { export const useAuth = () => {
const setToken = (token: string) => { const setToken = (token: string) => {