-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathproxy.ts
More file actions
28 lines (22 loc) · 936 Bytes
/
proxy.ts
File metadata and controls
28 lines (22 loc) · 936 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
27
28
import { NextRequest, NextResponse } from "next/server";
import { getSessionCookie } from "better-auth/cookies";
const unauthRoutes = ["/sign-in", "/sign-up", "/verify-2fa", "/reset-password"];
// THIS IS NOT SECURE!
// This is the recommended approach to optimistically redirect users
// We recommend handling auth checks in each page/route
export default async function middleware(request: NextRequest) {
const sessionCookie = getSessionCookie(request);
const isUnauthRoute = unauthRoutes.includes(request.nextUrl.pathname);
if (isUnauthRoute) {
return NextResponse.next();
}
if (!sessionCookie) {
console.log("middleware redirecting to sign-in");
return NextResponse.redirect(new URL("/sign-in", request.url));
}
return NextResponse.next();
}
export const config = {
// Run middleware on all routes except static assets and api routes
matcher: ["/((?!.*\\..*|_next|api/auth).+)", "/trpc(.*)"],
};