feat(auth): 添加用户认证模块与登录页面
This commit is contained in:
45
src/stores/auth-store-auto-imports.ts
Normal file
45
src/stores/auth-store-auto-imports.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
export const useAuthStore = defineStore('auth', () => {
|
||||
const token = useLocalStorage<string | null>('auth-token', null);
|
||||
const userInfo = ref<Record<string, any> | null>(null);
|
||||
|
||||
const isLoggedIn = computed(() => !!token.value);
|
||||
|
||||
function clearToken(reason?: string) {
|
||||
consola.info('🚮 [auth-store] clear: ', reason);
|
||||
token.value = null;
|
||||
userInfo.value = null;
|
||||
}
|
||||
|
||||
async function login(username: string, password: string) {
|
||||
// 模拟登录延迟
|
||||
await new Promise((resolve) => setTimeout(resolve, 500));
|
||||
|
||||
// 模拟验证
|
||||
if (username === 'admin' && password === 'admin') {
|
||||
token.value = `mock-token-${Date.now()}`;
|
||||
await fetchUserInfo();
|
||||
return { success: true };
|
||||
}
|
||||
|
||||
return { success: false, message: '用户名或密码错误' };
|
||||
}
|
||||
|
||||
async function fetchUserInfo() {
|
||||
if (!token.value) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 模拟获取用户信息延迟
|
||||
await new Promise((resolve) => setTimeout(resolve, 300));
|
||||
|
||||
// 模拟从服务器获取用户信息
|
||||
userInfo.value = {
|
||||
id: 1,
|
||||
username: 'admin',
|
||||
nickname: '管理员',
|
||||
roles: ['admin'],
|
||||
};
|
||||
}
|
||||
|
||||
return { token, isLoggedIn, userInfo, clearToken, login, fetchUserInfo };
|
||||
});
|
||||
Reference in New Issue
Block a user