forked from mirrors/homebox
2e82398e5c
* implement password score UI and functions * update strings tests to use `test`instead of `it` * update typing * refactor login/register UI+Logic * fix width on switches to properly display * fetch and store self in store * (WIP) unify card styles * update labels page * bump nuxt * use form area * use text area for description * unify confirm API * unify UI around pages * change header background height
49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
import { defineStore } from "pinia";
|
|
import { useLocalStorage } from "@vueuse/core";
|
|
import { UserApi } from "~~/lib/api/user";
|
|
import { UserOut } from "~~/lib/api/types/data-contracts";
|
|
|
|
export const useAuthStore = defineStore("auth", {
|
|
state: () => ({
|
|
token: useLocalStorage("pinia/auth/token", ""),
|
|
expires: useLocalStorage("pinia/auth/expires", ""),
|
|
self: null as UserOut | null,
|
|
}),
|
|
getters: {
|
|
isTokenExpired: state => {
|
|
if (!state.expires) {
|
|
return true;
|
|
}
|
|
|
|
if (typeof state.expires === "string") {
|
|
return new Date(state.expires) < new Date();
|
|
}
|
|
|
|
return state.expires < new Date();
|
|
},
|
|
},
|
|
actions: {
|
|
async logout(api: UserApi) {
|
|
const result = await api.logout();
|
|
|
|
if (result.error) {
|
|
return result;
|
|
}
|
|
|
|
this.token = "";
|
|
this.expires = "";
|
|
this.self = null;
|
|
|
|
return result;
|
|
},
|
|
/**
|
|
* clearSession is used when the user cannot be logged out via the API and
|
|
* must clear it's local session, usually when a 401 is received.
|
|
*/
|
|
clearSession() {
|
|
this.token = "";
|
|
this.expires = "";
|
|
navigateTo("/");
|
|
},
|
|
},
|
|
});
|