ARTICLE AD BOX
I'm getting this error and I really don't know where to start debugging. Can someone help me?
My system was working fine at first. I was trying to add a user package to the cookie token to make the user Premium, and it failed.
Could someone guide me on how to debug and check where the error is? I tried removing the middleware and everything worked normally again.
error image

my middleware.ts
const handleI18nRouting = createMiddleware(routing); function detectLocale(req: NextRequest) { const country = req.headers.get('x-vercel-ip-country'); if (country === 'VN') return 'vi'; if (country === 'CN') return 'zh'; // fallback when running locally const language = req.headers.get('accept-language'); if (language?.startsWith('vi')) return 'vi'; if (language?.startsWith('zh')) return 'zh'; return 'en'; } export async function middleware(req: NextRequest) { const path = req.nextUrl.pathname; // bỏ qua api và static if ( path.startsWith('/api') || path.startsWith('/_next') || path.includes('.') ) { return NextResponse.next(); } // kiểm tra đã có locale chưa const hasLocale = /^\/(vi|zh)(\/|$)/.test(path); if (!hasLocale) { const locale = detectLocale(req); // english = root if (locale === 'en') { return handleI18nRouting(req); } const url = req.nextUrl.clone(); url.pathname = `/${locale}${path}`; return NextResponse.redirect(url); } console.log('4'); // verify session const session = await verifySession(); const isAdminRoute = Object.values(adminUrl).some( p => path === p || path.startsWith(`${p}/`), ); const isProtectedRoute = Object.values(protectedUrl).some( p => path === p || path.startsWith(`${p}/`), ); const authPages = [ '/authentication/sign-in', '/authentication/sign-up', '/authentication/forgot-password', ]; const isAuthPage = authPages.includes(path); if (!session && (isProtectedRoute || isAdminRoute)) { return NextResponse.redirect(new URL('/authentication/sign-in', req.url)); } if (session && isAdminRoute && !session.isAdmin) { return NextResponse.rewrite(new URL('/unauthorized', req.url)); } if (session && isAuthPage) { return NextResponse.redirect(new URL('/', req.url)); } return handleI18nRouting(req); } export const config = { matcher: ['/((?!api|_next|_vercel|.*\\..*).*)'], };verifySession.ts
import { cookies } from 'next/headers'; import { cache } from 'react'; import { decrypt } from './session'; type SessionPayload = { userId: string; isAdmin: boolean; subscriptionTier?: string; subscriptionExpiredAt?: string | Date; }; export const verifySession = cache(async () => { try { const cookie = (await cookies()).get('session')?.value; if (!cookie || typeof cookie !== 'string') return null; const session = (await decrypt(cookie)) as SessionPayload; if (!session?.userId) { return null; } const expiredAt = session.subscriptionExpiredAt ? new Date(session.subscriptionExpiredAt) : null; const isVip = expiredAt ? expiredAt > new Date() : false; return { isAuth: true, userId: session.userId, isAdmin: session.isAdmin, isVip, subscriptionTier: session.subscriptionTier ?? 'FREE', subscriptionExpiredAt: expiredAt, }; } catch (error) { console.error('verifySession error:', error); return null; } });decrypt.ts
export async function decrypt(token: string) { if (!token || typeof token !== 'string') { throw new Error('Invalid token input'); } try { const { payload } = await jwtDecrypt(token, ENC_KEY); if (!payload?.jws || typeof payload.jws !== 'string') { throw new Error('Invalid token structure'); } const { payload: verifiedPayload } = await jwtVerify(payload.jws, SIGN_KEY); return verifiedPayload; } catch (err) { console.error('DECRYPT ERROR:', err); throw err; } }