11import { type NextRequest , NextResponse } from 'next/server' ;
22import { jwtVerify } from 'jose' ;
3- import { createSecretKey } from 'node:crypto' ;
4- import { config as authConfig } from './lib/auth/auth' ;
53
6- const secretKey = createSecretKey ( process . env . JWT_SECRET ! , 'utf-8' ) ;
7-
8- // Define protected routes
9- const protectedRoutes = [ '/api' ] ;
4+ const protectedRoutes = [ '/dashboard' ] ;
105
116export async function middleware ( request : NextRequest ) {
127 const { pathname } = request . nextUrl ;
13- const sessionCookie = request . cookies . get ( authConfig . cookieName ) ?. value ;
8+ const sessionCookie = request . cookies . get ( 'sarge.session' ) ?. value ;
149
1510 // Check if the route requires authentication
1611 const isProtectedRoute = protectedRoutes . some ( ( route ) => pathname . startsWith ( route ) ) ;
@@ -19,8 +14,11 @@ export async function middleware(request: NextRequest) {
1914 let isAuthenticated = false ;
2015 if ( sessionCookie ) {
2116 try {
22- await jwtVerify ( sessionCookie , secretKey , {
23- issuer : authConfig . issuer ,
17+ // Create secret key directly in middleware (Edge Runtime compatible)
18+ const secret = new TextEncoder ( ) . encode ( process . env . JWT_SECRET ) ;
19+
20+ await jwtVerify ( sessionCookie , secret , {
21+ issuer : 'sargenu' ,
2422 } ) ;
2523 isAuthenticated = true ;
2624 } catch {
0 commit comments