-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
41 lines (32 loc) · 1.04 KB
/
middleware.ts
File metadata and controls
41 lines (32 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import { auth } from '@/lib/auth';
import { NextResponse } from 'next/server';
export default auth((req) => {
const { nextUrl, auth: session } = req;
const isLoggedIn = !!session?.user;
const isAuthPage = nextUrl.pathname.startsWith('/login');
const isApiRoute = nextUrl.pathname.startsWith('/api');
const isPublicRoute = nextUrl.pathname === '/';
// API 라우트는 별도 처리
if (isApiRoute) {
return NextResponse.next();
}
// 로그인 페이지 접근
if (isAuthPage) {
if (isLoggedIn) {
return NextResponse.redirect(new URL('/dashboard', nextUrl));
}
return NextResponse.next();
}
// 보호된 라우트 접근
if (!isLoggedIn && !isPublicRoute) {
return NextResponse.redirect(new URL('/login', nextUrl));
}
// 루트 페이지 접근 시 리다이렉트
if (isPublicRoute && isLoggedIn) {
return NextResponse.redirect(new URL('/dashboard', nextUrl));
}
return NextResponse.next();
});
export const config = {
matcher: ['/((?!_next/static|_next/image|favicon.ico|.*\\.svg$).*)'],
};