useLogout.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import { useAuthStore } from '~/stores/auth'
  2. import { useDetailStore } from '~/stores/detail'
  3. import { useVendorsStore } from '~/stores/vendors'
  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. case 'BRAND':
  22. return 'brand'
  23. default:
  24. // 3. 기본값은 인플루언서
  25. return 'influence'
  26. }
  27. }
  28. const logout = async () => {
  29. try {
  30. // 현재 로그인 타입 저장 (로그아웃 전에 미리 저장)
  31. const loginType = getLoginType()
  32. // auth store 초기화
  33. authStore.setLogout()
  34. // vendors store 초기화
  35. vendorsStore.reset()
  36. // detail store 초기화
  37. detailStore.reset()
  38. // localStorage 정리
  39. localStorage.removeItem('authStore')
  40. localStorage.removeItem('tempAccess')
  41. // 성공 메시지 표시
  42. $toast.success('로그아웃되었습니다.')
  43. // 로그인 타입에 따라 적절한 페이지로 리다이렉트
  44. await navigateTo({
  45. path: '/',
  46. query: { type: loginType }
  47. }, { replace: true })
  48. } catch (error) {
  49. console.error('로그아웃 중 오류 발생:', error)
  50. // 오류가 발생해도 로컬 상태는 정리
  51. const loginType = getLoginType()
  52. authStore.setLogout()
  53. vendorsStore.reset()
  54. detailStore.reset()
  55. localStorage.clear()
  56. await navigateTo({
  57. path: '/',
  58. query: { type: loginType }
  59. }, { replace: true })
  60. }
  61. }
  62. return {
  63. logout
  64. }
  65. }