45 lines
1.5 KiB
JavaScript
45 lines
1.5 KiB
JavaScript
import { useRecoilState, useRecoilValue } from 'recoil'
|
|
import { sessionStore } from '@/store/commonAtom'
|
|
import { globalLocaleStore } from '@/store/localeAtom'
|
|
import { stuffSearchState } from '@/store/stuffAtom'
|
|
import { useAxios } from '@/hooks/useAxios'
|
|
import { logout } from '@/lib/authActions'
|
|
import Cookies from 'js-cookie'
|
|
|
|
/**
|
|
* 로그아웃 처리 Hook
|
|
* - 로그아웃 API 호출 후 성공/실패 상관없이 로그아웃 처리
|
|
* - Header, ChangePasswordPop 등에서 공통으로 사용
|
|
*/
|
|
export function useLogout() {
|
|
const [sessionState] = useRecoilState(sessionStore)
|
|
const [stuffSearch, setStuffSearch] = useRecoilState(stuffSearchState)
|
|
const globalLocaleState = useRecoilValue(globalLocaleStore)
|
|
const { promisePost } = useAxios(globalLocaleState)
|
|
|
|
const logoutProcess = async () => {
|
|
const param = {
|
|
loginId: sessionState.userId,
|
|
requestId: Cookies.get('sessionId'), // 로그인 시 저장한 sessionId
|
|
}
|
|
|
|
// 로그아웃 API 호출 (실패해도 로그아웃 진행)
|
|
try {
|
|
await promisePost({ url: '/api/login/v1.0/logout', data: param })
|
|
} catch (error) {
|
|
console.log('logout api error:', error)
|
|
}
|
|
|
|
// 물건검색 상태 초기화
|
|
setStuffSearch({
|
|
...stuffSearch,
|
|
code: 'DELETE',
|
|
})
|
|
Cookies.remove('sessionId') // sessionId 쿠키 삭제
|
|
await logout() // iron-session 세션 파기
|
|
window.location.href = '/login' // 로그인 페이지로 이동 (full reload)
|
|
}
|
|
|
|
return { logoutProcess }
|
|
}
|