next-app-session icon indicating copy to clipboard operation
next-app-session copied to clipboard

Is that possible to support middleware in App Route?

Open wangwailok opened this issue 2 years ago • 1 comments

I want to do some protected route, but middlware in App Route seems cannot get the session on server.

`export default async function middleware(req: NextRequest) {

//always shows {} const session = useServerSession(); return intlMiddleware(req); }`

wangwailok avatar Dec 20 '23 04:12 wangwailok

yeah some errors in middleware " Cannot read properties of undefined (reading 'charCodeAt')"

Full code import { NextResponse } from "next/server"; import type { NextRequest } from "next/server"; import { session } from "./lib/session";

export default async function middleware(request: NextRequest) {

const token = await session().all(); console.log(token, 'token<--------------') const { pathname } = request.nextUrl;

if (accessToken && pathname === "/") { return NextResponse.redirect(new URL("/account/dashboard", request.url)); }

if (accessToken && pathname.startsWith("/account")) { return NextResponse.redirect(new URL("/login", request.url)); }

return NextResponse.next(); }

export const config = { matcher: ["/account/:path*", "/"],

solve this mr.package owner @wangwailok , @Zeryther @majidkuhail

ghost avatar May 23 '24 08:05 ghost