sweetscript / next-app-session

A Next.js App router secure server-side session library
MIT License
28 stars 3 forks source link

Is that possible to support middleware in App Route? #15

Open wangwailok opened 10 months ago

wangwailok commented 10 months ago

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); }`

ghost commented 5 months ago

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