Skip to content

Commit e7c8cc4

Browse files
장아영장아영
authored andcommitted
[#128] ♻️ create 'useSignOut' query
1 parent 8748e54 commit e7c8cc4

File tree

2 files changed

+35
-19
lines changed

2 files changed

+35
-19
lines changed

queries/useSignOut.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use client'
2+
3+
import { useRouter } from 'next/navigation'
4+
5+
import { useMutation } from '@tanstack/react-query'
6+
7+
import { SignOut } from '@/services/auth/auth'
8+
9+
export const useSignOutMutation = () => {
10+
const router = useRouter()
11+
12+
return useMutation({
13+
mutationFn: SignOut,
14+
onSuccess: (response: Response) => {
15+
if (!response.ok) {
16+
console.error('Logout Failed')
17+
alert('로그아웃 실패')
18+
return
19+
}
20+
21+
const result = response.json()
22+
console.log('로그아웃 성공', result)
23+
24+
alert('로그아웃 성공')
25+
router.push(`/sign-in`)
26+
},
27+
onError: (error: unknown) => {
28+
console.error('Logout Error:', error)
29+
alert('로그아웃 요청 중 오류가 발생했습니다')
30+
},
31+
})
32+
}

src/app/page.tsx

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,12 @@
11
'use client'
22

3-
import { useRouter } from 'next/navigation'
3+
import { useSignOutMutation } from 'queries/useSignOut'
44

55
export default function Home(): JSX.Element {
6-
const router = useRouter()
6+
const mutation = useSignOutMutation()
77

88
const handleLogout = async () => {
9-
try {
10-
const response = await fetch(`/api/auth/sign-out`, {
11-
method: 'POST',
12-
headers: { 'Content-Type': 'application/json' },
13-
})
14-
if (!response.ok) {
15-
console.error('Logout failed')
16-
alert('로그아웃 실패 프록시')
17-
return
18-
}
19-
console.log('Logout successful')
20-
alert('로그아웃 성공 프록시')
21-
router.push('/sign-in')
22-
} catch (error) {
23-
console.error('Logout error', error)
24-
alert('로그아웃 요청 중 오류 발생')
25-
}
9+
mutation.mutate()
2610
}
2711

2812
return (

0 commit comments

Comments
 (0)