-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ts
More file actions
29 lines (22 loc) · 757 Bytes
/
middleware.ts
File metadata and controls
29 lines (22 loc) · 757 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
29
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { getSessionCookie } from 'better-auth/cookies';
const publicRoutes = ['/auth/signin', '/auth/signup'];
export function middleware(request: NextRequest) {
const pathname = request.nextUrl.pathname;
if (publicRoutes.includes(pathname)) {
return NextResponse.next();
}
const sessionCookie = getSessionCookie(request);
if (sessionCookie) {
return NextResponse.next();
}
const signInUrl = new URL('/auth/signin', request.url);
signInUrl.searchParams.set('callbackUrl', pathname);
return NextResponse.redirect(signInUrl);
}
export const config = {
matcher: [
'/((?!_next|static|favicon.ico|robots.txt|api/auth).*)',
],
};