Skip to content

Commit 7098843

Browse files
committed
feat: 최초 페이지 접근 시 랜딩 페이지로 리다이렉트
1 parent 9a4be89 commit 7098843

4 files changed

Lines changed: 36 additions & 1 deletion

File tree

app/(with-sidebar)/layout.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff 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

components/auth/LoginForm.tsx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff 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);

components/common/Sidebar.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff 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) {

middleware.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
};

0 commit comments

Comments
 (0)