dev #755
38
src/hooks/useLogout.js
Normal file
38
src/hooks/useLogout.js
Normal file
@ -0,0 +1,38 @@
|
||||
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'
|
||||
|
||||
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'),
|
||||
}
|
||||
await promisePost({ url: '/api/login/v1.0/logout', data: param })
|
||||
.then(async (res) => {
|
||||
if (res.data.result.resultCode === 'S') {
|
||||
setStuffSearch({
|
||||
...stuffSearch,
|
||||
code: 'DELETE',
|
||||
})
|
||||
Cookies.remove('sessionId')
|
||||
await logout()
|
||||
window.location.href = '/login'
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
alert(error.response?.data?.message || 'ログアウトに失敗しました。')
|
||||
})
|
||||
}
|
||||
|
||||
return { logoutProcess }
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user