auth enhancements amongst other things
This commit is contained in:
parent
bb9769e917
commit
85c3799fca
21 changed files with 336 additions and 363 deletions
|
|
@ -1,71 +0,0 @@
|
|||
import { reactive, toRefs } from 'vue';
|
||||
import { useIpc } from './ipc';
|
||||
|
||||
interface AuthState {
|
||||
accessToken: string | null;
|
||||
error?: Error;
|
||||
}
|
||||
|
||||
const state = reactive<AuthState>({
|
||||
accessToken: null,
|
||||
error: undefined,
|
||||
});
|
||||
|
||||
const AUTH_KEY = 'crimata_token';
|
||||
|
||||
let token: string | null;
|
||||
|
||||
token = window.localStorage.getItem(AUTH_KEY);
|
||||
if (token) {
|
||||
state.accessToken = token;
|
||||
window.localStorage.setItem(AUTH_KEY, token);
|
||||
}
|
||||
|
||||
// Token authentication.
|
||||
const authToken = async () => {
|
||||
console.log("Calling auth token!!")
|
||||
const { invoke } = useIpc();
|
||||
try {
|
||||
token = window.localStorage.getItem(AUTH_KEY);
|
||||
|
||||
if (!token) {
|
||||
token = "no-token"
|
||||
}
|
||||
|
||||
console.log(`Token ${token}`)
|
||||
const res = await invoke('auth-session', token);
|
||||
window.localStorage.setItem(AUTH_KEY, res);
|
||||
state.accessToken = res;
|
||||
}
|
||||
catch(e) {
|
||||
state.error = e;
|
||||
console.log('ERROR: failed authentication');
|
||||
window.localStorage.removeItem(AUTH_KEY);
|
||||
state.accessToken = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Gets called on socket open.
|
||||
window.ipcRenderer.on("auth", async (_event, _arg) => {
|
||||
await authToken();
|
||||
});
|
||||
|
||||
export const useAuth = () => {
|
||||
|
||||
const setToken = (token: string) => {
|
||||
window.localStorage.setItem(AUTH_KEY, token);
|
||||
state.accessToken = token;
|
||||
state.error = undefined;
|
||||
}
|
||||
|
||||
const logout = (): Promise<null> => {
|
||||
window.localStorage.removeItem(AUTH_KEY);
|
||||
return Promise.resolve(state.accessToken = null);
|
||||
}
|
||||
|
||||
return {
|
||||
setToken,
|
||||
logout,
|
||||
...toRefs(state), // accessToken, error
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue