diff --git a/src/lib/api.ts b/src/lib/api.ts index c7fad46..5206cf7 100644 --- a/src/lib/api.ts +++ b/src/lib/api.ts @@ -29,11 +29,25 @@ export const login = async (pin: string) => { return response.data; }; +export const logout = async (accessToken: string) => { + const response = await api.delete('/user/logout', { + data: { accessToken } + }); + return response.data; +} + export const adminLogin = async (loginId: string) => { const response = await api.post('/admin/login', { loginId }); return response.data; }; +export const adminLogout = async (accessToken: string) => { + const response = await api.delete('/admin/logout', { + data: { accessToken } + }); + return response.data; +}; + // Admin endpoints export const getAdminQuestionSets = async () => { const response = await api.get('/admin/questionsets'); diff --git a/src/store/auth.ts b/src/store/auth.ts index 91c8417..0333622 100644 --- a/src/store/auth.ts +++ b/src/store/auth.ts @@ -1,5 +1,6 @@ import { create } from 'zustand'; import { persist } from 'zustand/middleware'; +import { logout, adminLogout } from '../lib/api'; interface AuthState { token: string | null; @@ -10,16 +11,29 @@ interface AuthState { export const useAuthStore = create()( persist( - (set) => ({ + (set, get) => ({ token: null, isAdmin: false, setToken: (token, isAdmin) => { localStorage.setItem('token', token); set({ token, isAdmin }); }, - logout: () => { - localStorage.removeItem('token'); - set({ token: null, isAdmin: false }); + logout: async () => { + try { + const token = get().token; + if (token) { + if (get().isAdmin) { + await adminLogout(token); + } else { + await logout(token); + } + } + } catch (error) { + console.error('Error during logout:', error); + } finally { + localStorage.removeItem('token'); + set({ token: null, isAdmin: false }); + } }, }), {