| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- import { useAuthStore } from '~/stores/auth'
- import { useVendorsStore } from '~/stores/vendors'
- import { useDetailStore } from '~/stores/detail'
- export const useLogout = () => {
- const authStore = useAuthStore()
- const vendorsStore = useVendorsStore()
- const detailStore = useDetailStore()
- const { $toast } = useNuxtApp()
- const getLoginType = () => {
- // 1. snsTempData에서 먼저 확인
- if (authStore.auth.snsTempData?.logintype) {
- return authStore.auth.snsTempData.logintype
- }
-
- // 2. memberType 기반으로 판단
- const memberType = authStore.auth.memberType?.toUpperCase()
- switch (memberType) {
- case 'VENDOR':
- return 'vendor'
- case 'INFLUENCER':
- return 'influence'
- default:
- // 3. 마지막으로 companyId 존재 여부로 판단
- return authStore.auth.companyId ? 'vendor' : 'influence'
- }
- }
- const logout = async () => {
- try {
- // 현재 로그인 타입 저장 (로그아웃 전에 미리 저장)
- const loginType = getLoginType()
-
- // auth store 초기화
- authStore.setLogout()
-
- // vendors store 초기화
- vendorsStore.reset()
-
- // detail store 초기화
- detailStore.reset()
-
- // localStorage 정리
- localStorage.removeItem('authStore')
- localStorage.removeItem('tempAccess')
-
- // 성공 메시지 표시
- $toast.success('로그아웃되었습니다.')
-
- // 로그인 타입에 따라 적절한 페이지로 리다이렉트
- await navigateTo({
- path: '/',
- query: { type: loginType }
- }, { replace: true })
- } catch (error) {
- console.error('로그아웃 중 오류 발생:', error)
- // 오류가 발생해도 로컬 상태는 정리
- const loginType = getLoginType()
- authStore.setLogout()
- vendorsStore.reset()
- detailStore.reset()
- localStorage.clear()
- await navigateTo({
- path: '/',
- query: { type: loginType }
- }, { replace: true })
- }
- }
-
- return {
- logout
- }
- }
|