|
1 | | -import { NextRequest, NextResponse } from 'next/server'; |
2 | | -import { getBetterAuthSession } from '@/lib/auth/server-auth'; |
3 | | - |
4 | | -const protectedRoutes = ['/dashboard', '/user', '/admin']; |
5 | | - |
6 | | -const authRoutes = ['/auth', '/auth/signup', '/auth/forgot-password']; |
7 | | - |
8 | | -export async function proxy(req: NextRequest) { |
9 | | - const { pathname } = req.nextUrl; |
10 | | - |
11 | | - const cookieHeader = req.headers.get('cookie') || ''; |
12 | | - let isAuthenticated = false; |
13 | | - try { |
14 | | - const betterAuthSession = await getBetterAuthSession(cookieHeader); |
15 | | - if (betterAuthSession?.user) { |
16 | | - isAuthenticated = true; |
17 | | - } |
18 | | - } catch { |
19 | | - isAuthenticated = false; |
20 | | - } |
21 | | - |
22 | | - const isProtectedRoute = protectedRoutes.some(route => |
23 | | - pathname.startsWith(route) |
24 | | - ); |
25 | | - |
26 | | - const isAuthRoute = authRoutes.some(route => pathname.startsWith(route)); |
27 | | - |
28 | | - if (isAuthRoute && isAuthenticated) { |
29 | | - return NextResponse.redirect(new URL('/', req.url)); |
30 | | - } |
31 | | - |
32 | | - if (isProtectedRoute && !isAuthenticated) { |
33 | | - const signinUrl = new URL('/auth', req.url); |
34 | | - signinUrl.searchParams.set('callbackUrl', pathname); |
35 | | - return NextResponse.redirect(signinUrl); |
| 1 | +import { getSessionCookie } from 'better-auth/cookies'; |
| 2 | +import type { NextRequest } from 'next/server'; |
| 3 | +import { NextResponse } from 'next/server'; |
| 4 | + |
| 5 | +export async function proxy(request: NextRequest) { |
| 6 | + const cookies = getSessionCookie(request); |
| 7 | + if (!cookies) { |
| 8 | + return NextResponse.redirect(new URL('/auth?mode=signin', request.url)); |
36 | 9 | } |
37 | | - |
38 | | - // if (isOtherUserProfile && !isAuthenticated) { |
39 | | - // const signinUrl = new URL('/auth', req.url); |
40 | | - // signinUrl.searchParams.set('callbackUrl', pathname); |
41 | | - // return NextResponse.redirect(signinUrl); |
42 | | - // } |
43 | | - |
44 | 10 | return NextResponse.next(); |
45 | 11 | } |
46 | 12 |
|
47 | 13 | export const config = { |
48 | | - matcher: [ |
49 | | - /* |
50 | | - * Match all request paths except for the ones starting with: |
51 | | - * - api (API routes) |
52 | | - * - _next/static (static files) |
53 | | - * - _next/image (image optimization files) |
54 | | - * - favicon.ico (favicon file) |
55 | | - * - public folder |
56 | | - */ |
57 | | - '/((?!api|_next/static|_next/image|favicon.ico|public).*)', |
58 | | - ], |
| 14 | + matcher: ['/organizations', '/me'], |
59 | 15 | }; |
0 commit comments