fix post window close auth bug
This commit is contained in:
parent
46a127c217
commit
6a19a96855
7 changed files with 91 additions and 82 deletions
|
|
@ -1,13 +1,13 @@
|
|||
import { reactive, watch, toRefs } from 'vue';
|
||||
import { reactive, toRefs } from 'vue';
|
||||
import { useIpc } from './ipc';
|
||||
|
||||
interface AuthState {
|
||||
access_token?: string | null;
|
||||
accessToken?: string | null;
|
||||
error?: Error;
|
||||
}
|
||||
|
||||
const state = reactive<AuthState>({
|
||||
access_token: undefined,
|
||||
accessToken: undefined,
|
||||
error: undefined,
|
||||
});
|
||||
|
||||
|
|
@ -15,20 +15,28 @@ const AUTH_KEY = 'crimata_token';
|
|||
|
||||
const token = window.localStorage.getItem(AUTH_KEY);
|
||||
|
||||
// authenticate on socket connect
|
||||
window.ipcRenderer.on("fetch-token", async (event, payload: null) => {
|
||||
const { data, invoke } = useIpc('auth-user');
|
||||
const authToken = async () => {
|
||||
const { data, invoke } = useIpc('auth-user');
|
||||
try {
|
||||
await invoke(token);
|
||||
state.access_token = data.value;
|
||||
state.accessToken = data.value;
|
||||
}
|
||||
catch(e) {
|
||||
state.error = e;
|
||||
console.log('ERROR: failed authentication');
|
||||
window.localStorage.removeItem(AUTH_KEY);
|
||||
}
|
||||
}
|
||||
|
||||
// authenticate on socket connect
|
||||
window.ipcRenderer.on("fetch-token", async (_event, _arg) => {
|
||||
authToken();
|
||||
});
|
||||
|
||||
if (token) {
|
||||
authToken();
|
||||
}
|
||||
|
||||
export const useAuth = () => {
|
||||
const setToken = (token: string, remember: boolean) => {
|
||||
if (remember) {
|
||||
|
|
@ -36,18 +44,18 @@ export const useAuth = () => {
|
|||
window.localStorage.setItem(AUTH_KEY, token);
|
||||
}
|
||||
|
||||
state.access_token = token;
|
||||
state.accessToken = token;
|
||||
state.error = undefined;
|
||||
}
|
||||
|
||||
const logout = (): Promise<void> => {
|
||||
window.localStorage.removeItem(AUTH_KEY);
|
||||
return Promise.resolve(state.access_token = undefined);
|
||||
return Promise.resolve(state.accessToken = undefined);
|
||||
}
|
||||
|
||||
return {
|
||||
setToken,
|
||||
logout,
|
||||
...toRefs(state), // access_token, error
|
||||
...toRefs(state), // accessToken, error
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue