useLogout.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import { useAuthStore } from '~/stores/auth'
  2. import { useVendorsStore } from '~/stores/vendors'
  3. import { useDetailStore } from '~/stores/detail'
  4. export const useLogout = () => {
  5. const authStore = useAuthStore()
  6. const vendorsStore = useVendorsStore()
  7. const detailStore = useDetailStore()
  8. const { $toast } = useNuxtApp()
  9. const getLoginType = () => {
  10. // 1. snsTempData에서 먼저 확인
  11. if (authStore.auth.snsTempData?.logintype) {
  12. return authStore.auth.snsTempData.logintype
  13. }
  14. // 2. memberType 기반으로 판단
  15. const memberType = authStore.auth.memberType?.toUpperCase()
  16. switch (memberType) {
  17. case 'VENDOR':
  18. return 'vendor'
  19. case 'INFLUENCER':
  20. return 'influence'
  21. default:
  22. // 3. 마지막으로 companyId 존재 여부로 판단
  23. return authStore.auth.companyId ? 'vendor' : 'influence'
  24. }
  25. }
  26. const logout = async () => {
  27. try {
  28. // 현재 로그인 타입 저장 (로그아웃 전에 미리 저장)
  29. const loginType = getLoginType()
  30. // auth store 초기화
  31. authStore.setLogout()
  32. // vendors store 초기화
  33. vendorsStore.reset()
  34. // detail store 초기화
  35. detailStore.reset()
  36. // localStorage 정리
  37. localStorage.removeItem('authStore')
  38. localStorage.removeItem('tempAccess')
  39. // 성공 메시지 표시
  40. $toast.success('로그아웃되었습니다.')
  41. // 로그인 타입에 따라 적절한 페이지로 리다이렉트
  42. await navigateTo({
  43. path: '/',
  44. query: { type: loginType }
  45. }, { replace: true })
  46. } catch (error) {
  47. console.error('로그아웃 중 오류 발생:', error)
  48. // 오류가 발생해도 로컬 상태는 정리
  49. const loginType = getLoginType()
  50. authStore.setLogout()
  51. vendorsStore.reset()
  52. detailStore.reset()
  53. localStorage.clear()
  54. await navigateTo({
  55. path: '/',
  56. query: { type: loginType }
  57. }, { replace: true })
  58. }
  59. }
  60. return {
  61. logout
  62. }
  63. }