feat: add admin login

This commit is contained in:
yanghao05
2025-04-16 19:55:32 +08:00
parent 8d601d456e
commit e95fc65f5f
11 changed files with 241 additions and 3 deletions

View File

@@ -0,0 +1,31 @@
import { authService } from '@/api/authService';
import { defineStore } from 'pinia';
import { computed, ref } from 'vue';
export const useAuthStore = defineStore('auth', () => {
const token = ref(localStorage.getItem('token'));
const user = ref(null);
const isAuthenticated = computed(() => !!token.value);
async function login(username, password) {
const { data } = await authService.login(username, password);
token.value = data.token;
user.value = data.user;
localStorage.setItem('token', data.token);
}
function logout() {
token.value = null;
user.value = null;
localStorage.removeItem('token');
}
return {
token,
user,
isAuthenticated,
login,
logout
};
});