import { defineStore } from 'pinia'; import { login, logout, woIsMe } from '@/api'; import CookiesApi from '@/util/Cookies.ts'; export const UseAuthStore = defineStore('auth', { state: () => ({ username: '', password: '', isAdmin: false, isLogin: false, userId: '', prermissions: Array(), roles: Array(), token: '', isRemberMe: false, }), actions: { login(username: string, password: string) { login({ username, password }).then((r) => { if (this.isRemberMe) { this.username = username; this.password = password; } this.token = r.json().data; this.isLogin = true; this.woIsMe(); }); }, woIsMe() { if (this.isLogin) woIsMe() .then((r) => { const data = r.json().data; this.username = data.username; this.isAdmin = data.admin; this.prermissions = data.prermissions; this.roles = data.roles; this.userId = data.id; }) .catch(() => this.$reset()); }, logout() { logout().then(() => this.$reset()); }, }, persist: { storage: CookiesApi.StorageApi, }, });