useLogout.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import { useAuthStore } from '~/stores/auth'
  2. import { useVendorsStore } from '~/stores/vendors'
  3. import { useDetailStore } from '~/stores/detail'
  4. import { useRouter } from 'vue-router'
  5. export const useLogout = () => {
  6. const authStore = useAuthStore()
  7. const vendorsStore = useVendorsStore()
  8. const detailStore = useDetailStore()
  9. const router = useRouter()
  10. const { $toast } = useNuxtApp()
  11. const logout = async () => {
  12. try {
  13. // 현재 로그인 타입 저장 (로그아웃 전에 미리 저장)
  14. const loginType = authStore.auth.snsTempData?.logintype || 'vendor' // 기본값은 vendor
  15. // API 호출로 서버 세션 종료 (선택적)
  16. await useAxios().post('/api/auth/logout')
  17. // auth store 초기화
  18. authStore.setLogout()
  19. // vendors store 초기화
  20. vendorsStore.$reset()
  21. // detail store 초기화
  22. detailStore.$reset()
  23. // localStorage 정리
  24. localStorage.removeItem('authStore')
  25. localStorage.removeItem('tempAccess')
  26. // 성공 메시지 표시
  27. $toast.success('로그아웃되었습니다.')
  28. // 로그인 타입에 따라 적절한 페이지로 리다이렉트
  29. router.push(`/?type=${loginType}`)
  30. } catch (error) {
  31. console.error('로그아웃 중 오류 발생:', error)
  32. // 오류가 발생해도 로컬 상태는 정리
  33. const loginType = authStore.auth.snsTempData?.logintype || 'vendor'
  34. authStore.setLogout()
  35. vendorsStore.$reset()
  36. detailStore.$reset()
  37. localStorage.clear()
  38. router.push(`/?type=${loginType}`)
  39. }
  40. }
  41. return {
  42. logout
  43. }
  44. }