90 lines
2.0 KiB
TypeScript
90 lines
2.0 KiB
TypeScript
/**
|
|
* 인증 상태 관리 composable
|
|
*/
|
|
|
|
interface User {
|
|
employeeId: number
|
|
employeeName: string
|
|
employeeEmail: string
|
|
employeePosition: string | null
|
|
}
|
|
|
|
// 전역 상태
|
|
const currentUser = ref<User | null>(null)
|
|
const isLoading = ref(false)
|
|
|
|
export function useAuth() {
|
|
/**
|
|
* 현재 로그인 사용자 조회
|
|
*/
|
|
async function fetchCurrentUser(): Promise<User | null> {
|
|
try {
|
|
isLoading.value = true
|
|
const response = await $fetch<{ user: User | null }>('/api/auth/current-user')
|
|
currentUser.value = response.user
|
|
return response.user
|
|
} catch (error) {
|
|
currentUser.value = null
|
|
return null
|
|
} finally {
|
|
isLoading.value = false
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 이메일+이름으로 로그인
|
|
*/
|
|
async function login(email: string, name: string): Promise<User> {
|
|
const response = await $fetch<{ user: User }>('/api/auth/login', {
|
|
method: 'POST',
|
|
body: { email, name }
|
|
})
|
|
currentUser.value = response.user
|
|
return response.user
|
|
}
|
|
|
|
/**
|
|
* 기존 사용자 선택 로그인
|
|
*/
|
|
async function selectUser(employeeId: number): Promise<User> {
|
|
const response = await $fetch<{ user: User }>('/api/auth/select-user', {
|
|
method: 'POST',
|
|
body: { employeeId }
|
|
})
|
|
currentUser.value = response.user
|
|
return response.user
|
|
}
|
|
|
|
/**
|
|
* 최근 로그인 사용자 목록
|
|
*/
|
|
async function getRecentUsers(): Promise<User[]> {
|
|
const response = await $fetch<{ users: User[] }>('/api/auth/recent-users')
|
|
return response.users
|
|
}
|
|
|
|
/**
|
|
* 로그아웃
|
|
*/
|
|
async function logout(): Promise<void> {
|
|
await $fetch('/api/auth/logout', { method: 'POST' })
|
|
currentUser.value = null
|
|
}
|
|
|
|
/**
|
|
* 로그인 여부 확인
|
|
*/
|
|
const isLoggedIn = computed(() => currentUser.value !== null)
|
|
|
|
return {
|
|
currentUser: readonly(currentUser),
|
|
isLoading: readonly(isLoading),
|
|
isLoggedIn,
|
|
fetchCurrentUser,
|
|
login,
|
|
selectUser,
|
|
getRecentUsers,
|
|
logout
|
|
}
|
|
}
|