|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useEffect, useMemo } from 'react'; |
| 4 | +import { useUserProfileQuery } from '@/entities/user/model/use-user-profile-query'; |
| 5 | +import { useAuth } from '@/hooks/common/use-auth'; |
| 6 | +import { usePhoneVerificationStore } from './store'; |
| 7 | + |
| 8 | +/** |
| 9 | + * @deprecated usePhoneVerificationStore를 직접 사용하지 마세요. |
| 10 | + * 대신 usePhoneVerificationStatus 훅을 사용하세요. |
| 11 | + * |
| 12 | + * 이유: |
| 13 | + * - usePhoneVerificationStore는 localStorage 기반으로 서버 상태와 불일치할 수 있음 |
| 14 | + * - usePhoneVerificationStatus는 서버 상태를 단일 진실 공급원으로 사용 |
| 15 | + * |
| 16 | + * @see usePhoneVerificationStatus |
| 17 | + */ |
| 18 | +export { usePhoneVerificationStore } from './store'; |
| 19 | + |
| 20 | +/** |
| 21 | + * 본인인증 상태를 서버 데이터와 동기화하여 반환하는 훅 |
| 22 | + * |
| 23 | + * 문제: |
| 24 | + * - Zustand의 localStorage 기반 상태는 다른 브라우저/기기에서의 인증 상태를 반영하지 못함 |
| 25 | + * - localStorage가 삭제되면 인증이 풀린 것처럼 보임 |
| 26 | + * - 다른 계정이 같은 번호로 인증하면 기존 계정의 인증이 해제되지만 클라이언트는 모름 |
| 27 | + * |
| 28 | + * 해결: |
| 29 | + * - 서버의 userProfile.isVerified 또는 memberProfile.tel을 단일 진실 공급원으로 사용 |
| 30 | + * - Zustand 상태는 UI 반응성을 위한 캐시로만 활용 |
| 31 | + * - 서버 데이터로 Zustand 상태를 자동 동기화 |
| 32 | + * |
| 33 | + * @param overrideMemberId - 특정 memberId로 조회할 때 사용 (선택) |
| 34 | + */ |
| 35 | +export function usePhoneVerificationStatus(overrideMemberId?: number) { |
| 36 | + const { data: authData } = useAuth(); |
| 37 | + const memberId = overrideMemberId ?? authData?.memberId ?? null; |
| 38 | + |
| 39 | + const { data: userProfile, isLoading: isProfileLoading } = |
| 40 | + useUserProfileQuery(memberId ?? 0); |
| 41 | + |
| 42 | + const { |
| 43 | + isVerified: zustandIsVerified, |
| 44 | + phoneNumber: zustandPhoneNumber, |
| 45 | + setVerified, |
| 46 | + reset, |
| 47 | + } = usePhoneVerificationStore(); |
| 48 | + |
| 49 | + // 서버 데이터 기반 인증 상태 계산 |
| 50 | + const serverIsVerified = userProfile?.isVerified ?? false; |
| 51 | + const serverPhoneNumber = userProfile?.memberProfile?.tel ?? null; |
| 52 | + |
| 53 | + // 서버 데이터를 우선시하여 최종 인증 상태 결정 |
| 54 | + // 서버에 tel이 있으면 인증된 것으로 간주 (isVerified보다 tel 존재 여부가 더 확실한 지표) |
| 55 | + const isVerified = useMemo(() => { |
| 56 | + // 프로필 로딩 중일 때는 Zustand 상태를 임시로 사용 (UI 깜빡임 방지) |
| 57 | + if (isProfileLoading && !userProfile) { |
| 58 | + return zustandIsVerified; |
| 59 | + } |
| 60 | + |
| 61 | + // 서버에 전화번호가 있으면 인증 완료 |
| 62 | + if (serverPhoneNumber) { |
| 63 | + return true; |
| 64 | + } |
| 65 | + |
| 66 | + // 서버의 isVerified 플래그 확인 |
| 67 | + if (serverIsVerified) { |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + // 서버 데이터가 없으면 미인증 |
| 72 | + return false; |
| 73 | + }, [ |
| 74 | + isProfileLoading, |
| 75 | + userProfile, |
| 76 | + zustandIsVerified, |
| 77 | + serverPhoneNumber, |
| 78 | + serverIsVerified, |
| 79 | + ]); |
| 80 | + |
| 81 | + const phoneNumber = useMemo(() => { |
| 82 | + if (isProfileLoading && !userProfile) { |
| 83 | + return zustandPhoneNumber; |
| 84 | + } |
| 85 | + |
| 86 | + return serverPhoneNumber ?? null; |
| 87 | + }, [isProfileLoading, userProfile, zustandPhoneNumber, serverPhoneNumber]); |
| 88 | + |
| 89 | + // 서버 데이터와 Zustand 상태 동기화 |
| 90 | + useEffect(() => { |
| 91 | + if (isProfileLoading || !userProfile) return; |
| 92 | + |
| 93 | + if (serverPhoneNumber) { |
| 94 | + // 서버에 전화번호가 있으면 Zustand도 동기화 |
| 95 | + if (!zustandIsVerified || zustandPhoneNumber !== serverPhoneNumber) { |
| 96 | + setVerified(serverPhoneNumber); |
| 97 | + } |
| 98 | + } else if (!serverIsVerified) { |
| 99 | + // 서버에서 인증이 해제되었으면 Zustand도 초기화 |
| 100 | + if (zustandIsVerified) { |
| 101 | + reset(); |
| 102 | + } |
| 103 | + } |
| 104 | + }, [ |
| 105 | + isProfileLoading, |
| 106 | + userProfile, |
| 107 | + serverPhoneNumber, |
| 108 | + serverIsVerified, |
| 109 | + zustandIsVerified, |
| 110 | + zustandPhoneNumber, |
| 111 | + setVerified, |
| 112 | + reset, |
| 113 | + ]); |
| 114 | + |
| 115 | + return { |
| 116 | + isVerified, |
| 117 | + phoneNumber, |
| 118 | + isLoading: isProfileLoading, |
| 119 | + // 원본 상태들 (디버깅용) |
| 120 | + _serverIsVerified: serverIsVerified, |
| 121 | + _serverPhoneNumber: serverPhoneNumber, |
| 122 | + _zustandIsVerified: zustandIsVerified, |
| 123 | + _zustandPhoneNumber: zustandPhoneNumber, |
| 124 | + // Zustand 액션 (인증 완료 시 호출용) |
| 125 | + setVerified, |
| 126 | + reset, |
| 127 | + }; |
| 128 | +} |
0 commit comments