-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy.ts
More file actions
36 lines (29 loc) · 1.07 KB
/
proxy.ts
File metadata and controls
36 lines (29 loc) · 1.07 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
import type {NextRequest} from "next/server";
import {NextResponse} from "next/server";
import {getSessionCookie} from "better-auth/cookies";
import {routes} from "@/lib/routes";
export function proxy(request: NextRequest) {
const sessionCookie = getSessionCookie(request);
const {pathname} = request.nextUrl;
const isAuthPage = pathname === routes.signInPath || pathname === routes.signUpPath;
if (isAuthPage) {
if (sessionCookie) {
// Redirect logged-in users away from auth pages
return NextResponse.redirect(new URL(routes.homePath, request.url));
} else {
// Allow anonymous users to access auth pages
return NextResponse.next();
}
}
// Redirect anonymous users trying to access protected pages
if (!sessionCookie) {
return NextResponse.redirect(new URL(routes.signInPath, request.url));
}
return NextResponse.next();
}
export const config = {
matcher: [
'/((?!api|_next/static|_next/image|favicon.ico|assets).*)',
],
};
// export {proxy, config};