import { useAuthStore } from '~/stores/auth' import { useVendorsStore } from '~/stores/vendors' import { useDetailStore } from '~/stores/detail' import { useRouter } from 'vue-router' export const useLogout = () => { const authStore = useAuthStore() const vendorsStore = useVendorsStore() const detailStore = useDetailStore() const router = useRouter() const { $toast } = useNuxtApp() const logout = async () => { try { // 현재 로그인 타입 저장 (로그아웃 전에 미리 저장) const loginType = authStore.auth.snsTempData?.logintype || 'vendor' // 기본값은 vendor // API 호출로 서버 세션 종료 (선택적) await useAxios().post('/api/auth/logout') // auth store 초기화 authStore.setLogout() // vendors store 초기화 vendorsStore.$reset() // detail store 초기화 detailStore.$reset() // localStorage 정리 localStorage.removeItem('authStore') localStorage.removeItem('tempAccess') // 성공 메시지 표시 $toast.success('로그아웃되었습니다.') // 로그인 타입에 따라 적절한 페이지로 리다이렉트 router.push(`/?type=${loginType}`) } catch (error) { console.error('로그아웃 중 오류 발생:', error) // 오류가 발생해도 로컬 상태는 정리 const loginType = authStore.auth.snsTempData?.logintype || 'vendor' authStore.setLogout() vendorsStore.$reset() detailStore.$reset() localStorage.clear() router.push(`/?type=${loginType}`) } } return { logout } }