-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
26 lines (20 loc) · 883 Bytes
/
proxy.ts
File metadata and controls
26 lines (20 loc) · 883 Bytes
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
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
export function proxy(request: NextRequest) {
const session = request.cookies.get('session')?.value;
const { pathname } = request.nextUrl;
const isSignInPage = pathname === '/sign-in';
// If user is logged in and trying to access sign-in page, redirect to dashboard
if (isSignInPage && session) {
return NextResponse.redirect(new URL('/', request.url));
}
// If user is not logged in and not on sign-in page, redirect to sign-in
// We exclude static files and api routes from this check just in case, though the matcher handles most
if (!isSignInPage && !session) {
return NextResponse.redirect(new URL('/sign-in', request.url));
}
return NextResponse.next();
}
export const config = {
matcher: ['/((?!api|_next/static|_next/image|favicon.ico).*)'],
};