feat: 회원 탈퇴 (DELETE /v1/auth/me, 웹·앱 내 정보 시트 '탈퇴하기')
- 계정과 남긴 댓글·좋아요·저장을 한 트랜잭션으로 지우고 게시물·댓글 수를 맞춘다 - 로그아웃·탈퇴 때 내 정보 캐시를 먼저 버려 401 로 로그인 창이 뜨지 않게 - 탈퇴 테스트 추가, 공용 API 클라이언트 다시 생성
This commit is contained in:
1 parent
dd2d02758b
commit
86a0ea26ca
15 files changed
+313
-17
No files matched your search
@@ -1,6 +1,6 @@
|
||||
import { useInfiniteQuery, useMutation, useQuery, useQueryClient, type InfiniteData } from '@tanstack/react-query'
|
||||
import { useCallback, useDeferredValue, useEffect, useMemo, useRef, useState, type FormEvent, type ReactNode } from 'react'
|
||||
import { setTokens, subscribeAuth } from '@nalssigak/shared/api/auth'
|
||||
import { clearTokens, setTokens, subscribeAuth } from '@nalssigak/shared/api/auth'
|
||||
import { SITE_URL } from '../api/setup'
|
||||
import {
|
||||
addCommentMutation,
|
||||
@@ -18,6 +18,7 @@ import {
|
||||
signupMutation,
|
||||
unlikeCommentMutation,
|
||||
updateMeMutation,
|
||||
withdrawMutation,
|
||||
} from '@nalssigak/shared/api/client/@tanstack/react-query.gen'
|
||||
import type { CommentData, Factor, ResAuth, ResCommentList, ResIndexDetail } from '@nalssigak/shared/api/client/types.gen'
|
||||
import { track } from '../lib/analytics'
|
||||
@@ -862,7 +863,52 @@ function LoginSheet({ onDone, onLoggedIn }: { onDone: () => void; onLoggedIn: ()
|
||||
)
|
||||
}
|
||||
|
||||
/* ---------- 내 정보: 캐릭터·닉네임·로그아웃 ---------- */
|
||||
/* ---------- 내 정보: 캐릭터·닉네임·로그아웃·탈퇴 ---------- */
|
||||
/** 탈퇴: 한 번 더 묻고 지운다. 계정이 없어지니 이 기기 토큰도 지운다 */
|
||||
function Withdraw({ onDone }: { onDone: () => void }) {
|
||||
const withdraw = useMutation(withdrawMutation())
|
||||
const [asking, setAsking] = useState(false)
|
||||
const [failed, setFailed] = useState(false)
|
||||
if (!asking)
|
||||
return (
|
||||
<button type="button" onClick={() => setAsking(true)} className="cursor-pointer self-center p-1 text-[13px] font-semibold text-(--ink3)">
|
||||
탈퇴하기
|
||||
</button>
|
||||
)
|
||||
const run = () => {
|
||||
setFailed(false)
|
||||
withdraw.mutate(
|
||||
{},
|
||||
{
|
||||
onSuccess: (res) => {
|
||||
if (!res.result?.success) return setFailed(true)
|
||||
clearTokens()
|
||||
onDone()
|
||||
},
|
||||
onError: () => setFailed(true),
|
||||
},
|
||||
)
|
||||
}
|
||||
return (
|
||||
<div className="flex flex-col gap-2.5 rounded-[18px] bg-[#fff1f3] p-3.5" role="alert">
|
||||
<p className="m-0 text-center text-sm font-bold text-(--ink)">
|
||||
탈퇴하면 계정과 남긴 댓글·좋아요가
|
||||
<br />
|
||||
모두 지워져요. 되돌릴 수 없어요.
|
||||
</p>
|
||||
<div className="flex gap-2">
|
||||
<button type="button" onClick={() => setAsking(false)} disabled={withdraw.isPending} className="flex-1 cursor-pointer rounded-[14px] bg-white p-3 text-sm font-extrabold text-(--ink)">
|
||||
그만둘게요
|
||||
</button>
|
||||
<button type="button" onClick={run} disabled={withdraw.isPending} className="flex-1 cursor-pointer rounded-[14px] bg-(--heart) p-3 text-sm font-extrabold text-white disabled:opacity-60">
|
||||
탈퇴할게요
|
||||
</button>
|
||||
</div>
|
||||
{failed && <p className="m-0 text-center text-[13px] font-bold text-(--heart)">탈퇴하지 못했어요. 잠시 뒤에 다시 해 주세요.</p>}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
function MeSheet({ onDone }: { onDone: () => void }) {
|
||||
const { me, logout } = useAuth()
|
||||
const queryClient = useQueryClient()
|
||||
@@ -981,6 +1027,7 @@ function MeSheet({ onDone }: { onDone: () => void }) {
|
||||
>
|
||||
로그아웃
|
||||
</button>
|
||||
<Withdraw onDone={onDone} />
|
||||
</Body>
|
||||
)
|
||||
}
|
||||
|
||||
+11
-2
@@ -1,7 +1,7 @@
|
||||
import { useQuery, useQueryClient } from '@tanstack/react-query'
|
||||
import { useEffect, useMemo, useSyncExternalStore, type ReactNode } from 'react'
|
||||
import { clearTokens, getAccessToken, subscribeAuth } from '@nalssigak/shared/api/auth'
|
||||
import { meOptions } from '@nalssigak/shared/api/client/@tanstack/react-query.gen'
|
||||
import { meOptions, meQueryKey } from '@nalssigak/shared/api/client/@tanstack/react-query.gen'
|
||||
import { logout as logoutOnServer } from '@nalssigak/shared/api/client/sdk.gen'
|
||||
import { AuthContext } from './authContext'
|
||||
|
||||
@@ -18,7 +18,16 @@ export function AuthProvider({ children }: { children: ReactNode }) {
|
||||
const me = useQuery({ ...meOptions(), enabled: !!token })
|
||||
|
||||
// 로그인·로그아웃하면 좋아요 여부가 달라지니 받아 둔 데이터를 다시 받는다
|
||||
useEffect(() => subscribeAuth((reason) => reason === 'change' && queryClient.invalidateQueries()), [queryClient])
|
||||
// 로그아웃·탈퇴 땐 내 정보 캐시를 먼저 버린다. 남겨 두면 다시 받다가 401 이 나서 로그인 창이 뜬다
|
||||
useEffect(
|
||||
() =>
|
||||
subscribeAuth((reason) => {
|
||||
if (reason !== 'change') return
|
||||
if (!getAccessToken()) queryClient.removeQueries({ queryKey: meQueryKey() })
|
||||
void queryClient.invalidateQueries()
|
||||
}),
|
||||
[queryClient],
|
||||
)
|
||||
|
||||
const mePending = !!token && me.isPending
|
||||
const api = useMemo(() => ({ loggedIn: !!token, me: (token && me.data?.me) || null, mePending, logout }), [token, me.data, mePending])
|
||||
|
||||
Reference in new issue
Block a user