-
Notifications
You must be signed in to change notification settings - Fork 1
feat: 로그인 안한 상태로 페이지 접근 시 제한 #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| 'use client'; | ||
|
|
||
| import { useEffect, useState } from 'react'; | ||
| import { onAuthStateChanged, type User } from 'firebase/auth'; | ||
| import { useRouter } from 'next/navigation'; | ||
| import { auth } from '@/lib/firebase'; | ||
|
|
||
| type UseAuthGuardOptions = { | ||
| /** 로그인 안 됐을 때 보낼 경로 */ | ||
| redirectTo?: string; | ||
| }; | ||
|
|
||
| type UseAuthGuardResult = { | ||
| user: User | null; | ||
| loading: boolean; | ||
| isAuthed: boolean; | ||
| }; | ||
|
|
||
| export function useAuthGuard( | ||
| options: UseAuthGuardOptions = {} | ||
| ): UseAuthGuardResult { | ||
| const { redirectTo = '/login' } = options; | ||
|
DongEun02 marked this conversation as resolved.
|
||
|
|
||
| const router = useRouter(); | ||
| const [user, setUser] = useState<User | null>(null); | ||
| const [loading, setLoading] = useState(true); | ||
|
|
||
| useEffect(() => { | ||
| const unsub = onAuthStateChanged(auth, (u) => { | ||
| if (!u) { | ||
| setUser(null); | ||
| setLoading(true); | ||
| router.replace(redirectTo); | ||
| return; | ||
| } | ||
| setUser(u); | ||
| setLoading(false); | ||
| }); | ||
|
|
||
| return () => unsub(); | ||
| }, [router, redirectTo]); | ||
|
Comment on lines
+28
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
사용자가 미인증일 때
또한 Firebase 🔧 수정 제안 useEffect(() => {
const unsub = onAuthStateChanged(auth, (u) => {
if (!u) {
setUser(null);
- setLoading(true);
+ setLoading(false);
router.replace(redirectTo);
return;
}
setUser(u);
setLoading(false);
});
return () => unsub();
}, [router, redirectTo]);🤖 Prompt for AI Agents |
||
|
|
||
| return { user, loading, isAuthed: !!user }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| import { NextResponse } from 'next/server'; | ||
| import type { NextRequest } from 'next/server'; | ||
|
|
||
| export function middleware(request: NextRequest) { | ||
| const { pathname } = request.nextUrl; | ||
|
|
||
| // 쿠키에서 isLoggedIn 확인 | ||
| const isLoggedIn = request.cookies.get('isLoggedIn')?.value === 'true'; | ||
|
|
||
| if (pathname === '/') { | ||
| // 로그인이 안 된 경우에만 랜딩으로 보냄 | ||
| if (!isLoggedIn) { | ||
| return NextResponse.redirect(new URL('/landing', request.url)); | ||
| } | ||
| // 로그인 된 상태라면 그대로 루트('/')를 보여줌 | ||
| return NextResponse.next(); | ||
| } | ||
|
|
||
| if (pathname === '/landing' && isLoggedIn) { | ||
| return NextResponse.redirect(new URL('/', request.url)); | ||
| } | ||
|
|
||
| return NextResponse.next(); | ||
| } | ||
|
Comment on lines
+4
to
+24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: # Find the middleware file and related auth configuration
git ls-files | grep -E "(middleware|auth|cookie|firebase)" | head -20Repository: DeveloperBlog-Devflow/devflow Length of output: 428 🏁 Script executed: # Look for where isLoggedIn cookie is set
rg "isLoggedIn" -t typescript -t javascript -B 3 -A 3Repository: DeveloperBlog-Devflow/devflow Length of output: 107 🏁 Script executed: # Search for cookie configuration and HttpOnly settings
rg "cookies\." -t typescript -t javascript -B 2 -A 2 | head -50Repository: DeveloperBlog-Devflow/devflow Length of output: 107 🏁 Script executed: # Check for Firebase configuration
rg -i "firebase" -t typescript -t javascript -t json | head -20Repository: DeveloperBlog-Devflow/devflow Length of output: 107 🏁 Script executed: # Look for auth-related setup and session configuration
fd -e "ts" -e "js" -e "json" | xargs grep -l -E "(auth|session|login)" 2>/dev/null | head -10Repository: DeveloperBlog-Devflow/devflow Length of output: 160 🏁 Script executed: # Read the middleware file completely
cat -n middleware.tsRepository: DeveloperBlog-Devflow/devflow Length of output: 936 🏁 Script executed: # Look for where isLoggedIn cookie is set (likely in LoginForm)
cat -n "components/auth/LoginForm.tsx"Repository: DeveloperBlog-Devflow/devflow Length of output: 4636 🏁 Script executed: # Check the Firebase configuration
cat -n "lib/firebase.ts"Repository: DeveloperBlog-Devflow/devflow Length of output: 1373 🏁 Script executed: # Check signup service for auth handling
cat -n "services/auth/signup.service.ts"Repository: DeveloperBlog-Devflow/devflow Length of output: 1900 🏁 Script executed: # Search for any server-side auth handling or API routes
fd -e ts -e js | xargs grep -l "NextRequest\|NextResponse\|route.ts" 2>/dev/null | head -10Repository: DeveloperBlog-Devflow/devflow Length of output: 86 🏁 Script executed: # Check if there are any server-side auth routes or session handling
fd -e ts -e js "api.*auth\|api.*session\|route" 2>/dev/null | head -10Repository: DeveloperBlog-Devflow/devflow Length of output: 55 🏁 Script executed: # Look for any logout functionality or cookie clearing
rg "isLoggedIn|cookies\|logout" --type ts --type js -B 2 -A 2 | head -40Repository: DeveloperBlog-Devflow/devflow Length of output: 1324 보안 취약점: 클라이언트 쿠키 기반 라우팅 인증 우회 가능 현재 미들웨어는 이미 Firebase Auth를 통해 실제 인증을 처리하고 있으므로, 다음 중 하나의 방식으로 개선할 것을 권장합니다:
더불어, 명시적인 🔒 명시적 반환 추가 제안 if (pathname === '/landing' && isLoggedIn) {
return NextResponse.redirect(new URL('/', request.url));
}
+
+ return NextResponse.next();
}🤖 Prompt for AI Agents |
||
|
|
||
| export const config = { | ||
| matcher: ['/', '/landing'], | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.