@@ -2,35 +2,49 @@ import { useEffect } from 'react';
22import { useRouter } from 'next/router' ;
33import { useAuthStore } from '@/store/auth' ;
44import { ROUTES } from '@/constants' ;
5+ import { resolveSafeAuthRedirect } from '@/utils/auth-session' ;
6+
7+ /**
8+ * 인증 플로우 페이지들은 자체적으로 /interest 리다이렉트를 callbackUrl과 함께 처리한다.
9+ * useAuthSync가 이 페이지들에서 동작하면 race condition이 발생하므로 제외한다.
10+ */
11+ const AUTH_FLOW_PATHNAMES = new Set ( [
12+ ROUTES . LOGIN ,
13+ ROUTES . SIGNUP ,
14+ ROUTES . INTEREST ,
15+ '/auth/social/callback' ,
16+ ] ) ;
517
618/**
719 * 사용자 상태를 기준으로 관심사 등록 페이지 리다이렉트를 동기화한다.
20+ * 인증 플로우 페이지에서는 동작하지 않으며, 현재 페이지를 callbackUrl로 보존한다.
821 */
922export function useAuthSync ( ) {
1023 const router = useRouter ( ) ;
1124 const authState = useAuthStore ( ( state ) => state . authState ) ;
1225 const user = useAuthStore ( ( state ) => state . user ) ;
1326 const isBootstrapped = useAuthStore ( ( state ) => state . isBootstrapped ) ;
1427
15- // InterestSetting 확인 및 리다이렉트
1628 useEffect ( ( ) => {
1729 if ( ! isBootstrapped ) return ;
1830 if ( authState !== 'authenticated' || ! user ) return ;
1931
20- // InterestSetting이 false이고, 현재 페이지가 관심사 등록 페이지가 아니면 리다이렉트
2132 const skipInterestRedirect =
2233 typeof window !== 'undefined' && window . sessionStorage . getItem ( 'interest_done' ) === '1' ;
2334
2435 if ( user . InterestSetting !== false && skipInterestRedirect ) {
2536 window . sessionStorage . removeItem ( 'interest_done' ) ;
2637 }
2738
28- if (
29- user . InterestSetting === false &&
30- ! skipInterestRedirect &&
31- router . pathname !== ROUTES . INTEREST
32- ) {
33- void router . push ( ROUTES . INTEREST ) ;
39+ const isAuthFlowPage = AUTH_FLOW_PATHNAMES . has ( router . pathname ) ;
40+
41+ if ( user . InterestSetting === false && ! skipInterestRedirect && isAuthFlowPage === false ) {
42+ const currentPath = resolveSafeAuthRedirect ( router . asPath ) ;
43+ const interestUrl =
44+ currentPath !== ROUTES . HOME
45+ ? `${ ROUTES . INTEREST } ?callbackUrl=${ encodeURIComponent ( currentPath ) } `
46+ : ROUTES . INTEREST ;
47+ void router . replace ( interestUrl ) ;
3448 }
3549 } , [ authState , isBootstrapped , router , user ] ) ;
3650
0 commit comments