feat: complete V1 phases 0-7
This commit is contained in:
@@ -0,0 +1,39 @@
|
||||
import { defineStore } from 'pinia';
|
||||
import { computed, ref } from 'vue';
|
||||
|
||||
import type { AdminLoginResponse, AuthUser, CurrentUserResponse } from '@lehuo/contracts';
|
||||
|
||||
import { apiRequest } from '../api/api-client';
|
||||
|
||||
export const useAuthStore = defineStore('auth', () => {
|
||||
const user = ref<AuthUser | null>(null);
|
||||
const checking = ref(true);
|
||||
const authenticated = computed(() => user.value !== null);
|
||||
|
||||
async function restore(): Promise<void> {
|
||||
checking.value = true;
|
||||
try {
|
||||
const result = await apiRequest<CurrentUserResponse>('/api/v1/admin/auth/me');
|
||||
user.value = result.user;
|
||||
} catch {
|
||||
user.value = null;
|
||||
} finally {
|
||||
checking.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function login(username: string, password: string): Promise<void> {
|
||||
const result = await apiRequest<AdminLoginResponse>('/api/v1/admin/auth/login', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ username, password }),
|
||||
});
|
||||
user.value = result.user;
|
||||
}
|
||||
|
||||
async function logout(): Promise<void> {
|
||||
await apiRequest('/api/v1/admin/auth/logout', { method: 'POST', body: '{}' });
|
||||
user.value = null;
|
||||
}
|
||||
|
||||
return { user, checking, authenticated, restore, login, logout };
|
||||
});
|
||||
Reference in New Issue
Block a user