Replies: 2 comments
-
|
Maybe you can use this. https://github.com/honojs/hono/tree/main/src%2Fmiddleware%2Fcontext-storage |
Beta Was this translation helpful? Give feedback.
-
|
The Next.js middleware runs on the Edge and doesn't have access to Hono's context — they're separate execution layers. But you can still protect pages by reading the session cookie that Here are two approaches: Option 1: Read the session cookie directly in Next.js middleware
// middleware.ts
import { NextRequest, NextResponse } from 'next/server'
const protectedRoutes = ['/dashboard', '/settings', '/profile']
export function middleware(request: NextRequest) {
const sessionToken =
request.cookies.get('authjs.session-token')?.value ||
request.cookies.get('__Secure-authjs.session-token')?.value
const isProtected = protectedRoutes.some((route) =>
request.nextUrl.pathname.startsWith(route)
)
if (isProtected && !sessionToken) {
return NextResponse.redirect(new URL('/login', request.url))
}
return NextResponse.next()
}
export const config = {
matcher: ['/dashboard/:path*', '/settings/:path*', '/profile/:path*'],
}This is a lightweight check (cookie exists = probably logged in). It won't validate the JWT, but it handles the redirect fast on the Edge. Option 2: Use
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello everyone,
In my Next.js project, I use Hono for the API part and @hono/auth-js for authentication.
Now, I want to block certain pages in the Next.js middleware when the user is not logged in. Unfortunately, at this point, I don't have access to the Hono context.
Could someone help me solve this issue?
Thanks in advance.
Beta Was this translation helpful? Give feedback.
All reactions