File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -15,7 +15,6 @@ export default function SidebarLayout({
1515
1616 if ( loading ) return < div > 로딩중...</ div > ;
1717 if ( ! isAuthed ) {
18- alert ( '로그인이 필요합니다.' ) ;
1918 return null ;
2019 }
2120
Original file line number Diff line number Diff line change @@ -22,12 +22,19 @@ const LoginForm = ({ handleOpenModal }: LoginFormProps) => {
2222
2323 const router = useRouter ( ) ;
2424
25+ // 쿠키 설정 헬퍼 함수
26+ const setLoginCookie = ( ) => {
27+ // 7일간 유지되는 쿠키 설정
28+ document . cookie = 'isLoggedIn=true; path=/; max-age=' + 60 * 60 * 24 * 7 ;
29+ } ;
30+
2531 const handleGithubLogin = async ( ) => {
2632 if ( isLoading ) return ;
2733 setIsLoading ( true ) ;
2834 setError ( '' ) ;
2935 try {
3036 await signupWithGithub ( ) ;
37+ setLoginCookie ( ) ; // 쿠키 설정
3138 router . push ( '/' ) ;
3239 } catch ( err : unknown ) {
3340 setError ( 'Github 로그인에 실패했습니다.' ) ;
@@ -58,6 +65,7 @@ const LoginForm = ({ handleOpenModal }: LoginFormProps) => {
5865 password
5966 ) ;
6067 // console.log('로그인 성공!', userCredential.user);
68+ setLoginCookie ( ) ; // 쿠키 설정
6169 router . push ( '/' ) ;
6270 } catch ( err ) {
6371 // console.error('로그인 에러:', err);
Original file line number Diff line number Diff line change @@ -26,6 +26,8 @@ const Sidebar = () => {
2626 const handleLogout = async ( ) : Promise < void > => {
2727 try {
2828 await signOut ( auth ) ;
29+ document . cookie =
30+ 'isLoggedIn=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT' ;
2931 alert ( '로그아웃 되었습니다.' ) ;
3032 router . replace ( '/landing' ) ;
3133 } catch ( err ) {
Original file line number Diff line number Diff line change 1+ import { NextResponse } from 'next/server' ;
2+ import type { NextRequest } from 'next/server' ;
3+
4+ export function middleware ( request : NextRequest ) {
5+ const { pathname } = request . nextUrl ;
6+
7+ // 쿠키에서 isLoggedIn 확인
8+ const isLoggedIn = request . cookies . get ( 'isLoggedIn' ) ?. value === 'true' ;
9+
10+ if ( pathname === '/' ) {
11+ // 로그인이 안 된 경우에만 랜딩으로 보냄
12+ if ( ! isLoggedIn ) {
13+ return NextResponse . redirect ( new URL ( '/landing' , request . url ) ) ;
14+ }
15+ // 로그인 된 상태라면 그대로 루트('/')를 보여줌
16+ return NextResponse . next ( ) ;
17+ }
18+
19+ if ( pathname === '/landing' && isLoggedIn ) {
20+ return NextResponse . redirect ( new URL ( '/' , request . url ) ) ;
21+ }
22+ }
23+
24+ export const config = {
25+ matcher : [ '/' , '/landing' ] ,
26+ } ;
You can’t perform that action at this time.
0 commit comments