add persistent auth session
This commit is contained in:
parent
bf7c0a8445
commit
a7253ebf40
8 changed files with 69 additions and 66 deletions
|
|
@ -2,12 +2,12 @@ import { reactive, toRefs } from 'vue';
|
|||
import { useIpc } from './ipc';
|
||||
|
||||
interface AuthState {
|
||||
accessToken?: string | null;
|
||||
accessToken: string | null;
|
||||
error?: Error;
|
||||
}
|
||||
|
||||
const state = reactive<AuthState>({
|
||||
accessToken: undefined,
|
||||
accessToken: null,
|
||||
error: undefined,
|
||||
});
|
||||
|
||||
|
|
@ -16,7 +16,7 @@ const AUTH_KEY = 'crimata_token';
|
|||
const token = window.localStorage.getItem(AUTH_KEY);
|
||||
|
||||
const authToken = async () => {
|
||||
const { data, invoke } = useIpc('auth-user');
|
||||
const { data, invoke } = useIpc('auth-session');
|
||||
try {
|
||||
await invoke(token);
|
||||
state.accessToken = data.value;
|
||||
|
|
@ -28,29 +28,26 @@ const authToken = async () => {
|
|||
}
|
||||
}
|
||||
|
||||
// authenticate on socket connect
|
||||
window.ipcRenderer.on("fetch-token", async (_event, _arg) => {
|
||||
authToken();
|
||||
// authenticate on auth event
|
||||
window.ipcRenderer.on("auth", async (_event, _arg) => {
|
||||
await authToken();
|
||||
});
|
||||
|
||||
if (token) {
|
||||
authToken();
|
||||
state.accessToken = token;
|
||||
}
|
||||
|
||||
export const useAuth = () => {
|
||||
const setToken = (token: string, remember: boolean) => {
|
||||
if (remember) {
|
||||
// Save
|
||||
window.localStorage.setItem(AUTH_KEY, token);
|
||||
}
|
||||
|
||||
const setToken = (token: string) => {
|
||||
window.localStorage.setItem(AUTH_KEY, token);
|
||||
state.accessToken = token;
|
||||
state.error = undefined;
|
||||
}
|
||||
|
||||
const logout = (): Promise<void> => {
|
||||
const logout = (): Promise<null> => {
|
||||
window.localStorage.removeItem(AUTH_KEY);
|
||||
return Promise.resolve(state.accessToken = undefined);
|
||||
return Promise.resolve(state.accessToken = null);
|
||||
}
|
||||
|
||||
return {
|
||||
|
|
|
|||
Loading…
Reference in a new issue