Skip to content

Conversation

@sgoldenbird
Copy link
Contributor

@sgoldenbird sgoldenbird commented Aug 29, 2025

👻 관련 이슈 번호

  • none

👻 요약

  • user정보가 있을경우에만 API호출, 불필요한 401에러표시 방지

👻 주요 변경 사항

처음 웹사이트에 진입하거나 로그아웃하면 401에러가 개발자 도구 콘솔에 찍힌다.
사용자 입장에서는 보이지 않으니 심각한 문제는 아니지만 콘솔 노이즈을 줄이고 불필요한 API를 호출하지 않도록 수정

👻 체크리스트

  • Assignees에 본인을 등록했나요?
  • 라벨을 사이드 탭에서 등록했나요?
  • PR은 사이드 탭 Projects를 등록 하지마세요.
  • PR은 Milestone을 등록 하지마세요.
  • PR을 보내는 브랜치가 올바른지 확인했나요?
  • 팀원들이 리뷰하기 쉽도록 설명을 자세하게 작성했나요?
  • 변경사항을 충분히 테스트 했나요?
  • 컨벤션에 맞게 구현했나요?

📷 UI 변경 사항

👻 문제 사항

👻 논의 사항

👻 기타 참고 사항

Summary by CodeRabbit

  • 신기능
    • 해당 없음
  • 성능 개선
    • 로그인 상태가 없을 때 불필요한 세션 검증 요청을 차단하여 초기 로드가 빨라지고 데이터 사용량이 감소했습니다.
  • 버그 수정
    • 로그인 정보 부재로 인한 불필요한 재검증을 제거해 화면 깜빡임 및 잘못된 로그아웃 표시 현상을 줄였습니다.
  • 기타
    • 사용자 상태를 전역 스토어와 동기화하여 인증 검사 호출이 사용자 존재 시에만 이루어지도록 안전하게 조정했습니다.

@sgoldenbird sgoldenbird self-assigned this Aug 29, 2025
@sgoldenbird sgoldenbird linked an issue Aug 29, 2025 that may be closed by this pull request
7 tasks
@coderabbitai
Copy link

coderabbitai bot commented Aug 29, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

AuthInitializer와 useUser 훅이 Zustand 스토어의 user 존재 여부로 세션 검증/쿼리를 조건부 실행하도록 변경됨; refetch/useQuery는 user가 있을 때만 트리거되며 공개 시그니처는 불변입니다.

Changes

Cohort / File(s) Summary
Auth 세션 검증 조건부 실행
src/app/_components/AuthInitializer.tsx, src/domain/Auth/hooks/useUser.ts
Zustand의 useRoamReadyStore에서 user를 읽어 존재할 때만 세션 검증/쿼리를 실행하도록 변경 (AuthInitializeruser 존재 시에만 refetch() 호출, useUseruseQueryenabled: !!user). useEffect deps에 user 추가. 공개 시그니처 변경 없음.

Sequence Diagram(s)

sequenceDiagram
  actor User as 사용자
  participant App as App
  participant AuthInit as AuthInitializer
  participant Store as Zustand Store
  participant RQ as React Query
  participant API as Auth API

  User->>App: 앱 로드
  App->>AuthInit: 마운트
  AuthInit->>Store: user 조회
  alt user 존재
    AuthInit->>RQ: refetch() (세션 검증)
    RQ->>API: GET /session (또는 유사 엔드포인트)
    alt 성공
      RQ->>Store: setUser(서버 응답)
    else 실패
      RQ->>Store: clearUser()
    end
  else user 없음
    Note over AuthInit,RQ: 네트워크 요청 수행 안 함
  end
Loading
sequenceDiagram
  participant Component as 임의 컴포넌트
  participant useUser as useUser 훅
  participant Store as Zustand Store
  participant RQ as React Query
  participant API as Auth API

  Component->>useUser: 호출
  useUser->>Store: user 조회
  alt enabled: !!user
    useUser->>RQ: useQuery(enabled=true)
    RQ->>API: 사용자 데이터 조회
    alt 성공
      RQ->>Store: setUser()
    else 실패
      RQ->>Store: clearUser()
    end
  else enabled=false
    Note over useUser: 쿼리 비활성화 (대기)
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related issues

  • Fix: 첫 진입 시 401 문제 #558: 동일한 목표(사용자 미존재 시 초기 401/불필요한 API 호출 방지)로 세션 검증/리패치 조건부 실행을 다루므로 연관 가능.

Possibly related PRs

Suggested reviewers

  • Seon-K
  • Yongmin0423

Poem

새벽 코드 숲을 훌쩍 뛰어와,
유저 있으면 폴짝 refetch, 없으면 살짝 멈춰요.
쿼리 귀 기울여 성공에 춤추고, 실패면 조용히 정리해요.
토끼는 조건으로 달리고, 로그는 깔끔히 남깁니다. 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 9310db4 and 36be57d.

📒 Files selected for processing (2)
  • src/app/_components/AuthInitializer.tsx (2 hunks)
  • src/domain/Auth/hooks/useUser.ts (2 hunks)
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/558/fix401

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions
Copy link

✅ Preview Deployment Ready!

🔗 Preview URL: https://roam-ready-3iveqb2kh-yongmins-projects-bf5f7733.vercel.app
📝 Branch: feat/558/fix401
💾 Commit: Fix: 불필요한 401 방지
🕐 Deployed at:


This preview will be automatically updated on new commits.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (2)
src/domain/Auth/hooks/useUser.ts (2)

74-79: 모든 에러에서 clearUser() 호출 → 오프라인/서버 오류에도 강제 로그아웃 위험.

에러가 401/403일 때만 사용자 상태를 비우도록 제한하세요.

   useEffect(() => {
     // isError가 true가 되면 (데이터 로딩 실패 시)
     if (queryResult.isError) {
-      clearUser();
+      const status =
+        (queryResult.error as any)?.response?.status ??
+        (queryResult.error as any)?.status;
+      if (status === 401 || status === 403) {
+        clearUser();
+      }
     }
   }, [queryResult.isError, clearUser]);

57-64: retry 정책 보완 및 staleTime 옵션 추가 필요

  • enabled: !!user 설정은 AuthInitializer의 수동 refetch 로직으로 초기 인증 검증이 수행되므로 그대로 유지하세요.
  • 기본 retry: 1은 401/403을 포함한 모든 오류에 재시도를 수행하므로, 401·403 및 기타 4xx 에러를 차단하고 네트워크/5xx만 1회 재시도하도록 함수 형태로 변경해야 합니다.
  • JSDoc에 명시된 5분(staleTime) 옵션이 코드에 누락되어 있으므로 staleTime: 5 * 60 * 1000을 추가하세요.
   queryKey: ['user', 'me'],
   queryFn: getMe,
-  enabled: !!user,
-  retry: 1,
+  enabled: !!user,
+  staleTime: 5 * 60 * 1000,
+  retry: (failureCount, error) => {
+    const status =
+      (error as any)?.response?.status ?? (error as any)?.status;
+    // 인증 에러 및 4xx는 재시도하지 않음
+    if (status === 401 || status === 403 || (status && status < 500)) return false;
+    // 네트워크/5xx만 1회 재시도
+    return failureCount < 1;
+  },
   refetchOnWindowFocus: true,
   refetchOnReconnect: true,
🧹 Nitpick comments (3)
src/domain/Auth/hooks/useUser.ts (3)

66-73: onSuccess/onError로 부가 작업을 옮기면 의존성/재렌더 줄어듭니다.

효과를 훅 옵션으로 수렴해 사이드이펙트 흐름을 한 곳에서 관리하세요.

const queryResult = useQuery({
  queryKey: ['user', 'me'],
  queryFn: getMe,
  enabled: !!user,
  staleTime: 5 * 60 * 1000,
  retry: (failureCount, error) => { /* 위와 동일 */ },
  onSuccess: setUser,
  onError: (error) => {
    const s = (error as any)?.response?.status ?? (error as any)?.status;
    if (s === 401 || s === 403) clearUser();
  },
});

55-55: 스토어 구독 수 최소화(미세 최적화).

동일 스토어를 3번 구독 중입니다. 한 번의 셀렉터 + shallow 비교로 묶으면 리렌더를 줄일 수 있습니다.

import { shallow } from 'zustand/shallow';

const { user, setUser, clearUser } = useRoamReadyStore(
  (s) => ({ user: s.user, setUser: s.setUser, clearUser: s.clearUser }),
  shallow,
);

20-30: 문서-코드 불일치: staleTime 5분 설명 있으나 코드에 없음.

옵션을 추가(위 Diff 참조)하거나 주석을 수정하세요.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 560c358 and 9310db4.

📒 Files selected for processing (2)
  • src/app/_components/AuthInitializer.tsx (2 hunks)
  • src/domain/Auth/hooks/useUser.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
src/app/_components/AuthInitializer.tsx (1)
src/shared/store/index.ts (1)
  • useRoamReadyStore (32-47)
src/domain/Auth/hooks/useUser.ts (2)
src/shared/store/index.ts (1)
  • useRoamReadyStore (32-47)
src/domain/Auth/services/index.ts (1)
  • getMe (85-87)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: vercel-preview

Comment on lines +20 to +25
// 스토어에 사용자 정보가 있을 경우 (즉, 이전에 로그인했을 경우)에만
// 세션 유효성 검사를 위해 refetch를 실행합니다.
if (user) {
refetch();
}
}, [refetch, user]);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

⚠️ Potential issue

user 의존 refetch는 중복 호출·루프 위험 → 제거 권장.

  • useUser 내부에서 enabled: !!user로 이미 자동 fetch가 발생합니다.
  • useUser 성공 시 setUser가 호출되면 user 레퍼런스가 바뀌고, 본 useEffect가 다시 refetch를 불러 무한/과도한 요청으로 이어질 수 있습니다(중복 fetch 또는 fetch-→setUser→refetch 반복).

불필요한 refetch와 store 의존을 제거해 Initializer를 “훅 마운트만” 하도록 단순화하세요.

-'use client';
-
-import { useEffect } from 'react';
+'use client';
 
 import { useUser } from '@/domain/Auth/hooks/useUser';
-import { useRoamReadyStore } from '@/shared/store';
 
 export default function AuthInitializer() {
-  const { refetch } = useUser(); // 쿼리 객체를 받아서
-  const user = useRoamReadyStore((state) => state.user);
-
-  useEffect(() => {
-    // 스토어에 사용자 정보가 있을 경우 (즉, 이전에 로그인했을 경우)에만
-    // 세션 유효성 검사를 위해 refetch를 실행합니다.
-    if (user) {
-      refetch();
-    }
-  }, [refetch, user]);
+  // enabled: !!user 로 이미 내부에서 자동 실행됨
+  useUser();
   return null;
 }

대안(정 꼭 필요하다면): status/isFetching을 함께 받아 “idle이고 user가 생겼을 때 1회만” refetch 하도록 강하게 가드하세요.

const { refetch, status, isFetching } = useUser();
useEffect(() => {
  if (user && status === 'idle' && !isFetching) refetch();
}, [refetch, user, status, isFetching]);

Also applies to: 17-17, 6-6, 3-3

🤖 Prompt for AI Agents
In src/app/_components/AuthInitializer.tsx around lines 20 to 25, the useEffect
that calls refetch when user exists causes redundant/looping fetches because
useUser already auto-fetches with enabled: !!user and setUser changes user
reference; remove the user-dependent refetch to make the initializer only mount
the hook (delete the if (user) refetch() call and remove user from the effect
deps so effect runs only on mount/refetch identity), or if you must keep a
guarded refetch implement a one-time guard by also reading status/isFetching
from useUser and only calling refetch when user exists AND status === 'idle' AND
!isFetching so it runs once.

@sgoldenbird sgoldenbird merged commit 5978e96 into main Aug 29, 2025
1 of 2 checks passed
@sgoldenbird sgoldenbird deleted the feat/558/fix401 branch August 29, 2025 15:25
@github-actions
Copy link

✅ Preview Deployment Ready!

🔗 Preview URL: https://roam-ready-a431w5x5z-yongmins-projects-bf5f7733.vercel.app
📝 Branch: feat/558/fix401
💾 Commit: Fix: 불필요한 401 방지
🕐 Deployed at:


This preview will be automatically updated on new commits.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fix: 첫 진입 시 401 문제

2 participants