Open contd opened 2 years ago
This should fix it: https://stackoverflow.com/questions/73040090/nextjs-middleware-does-not-seem-to-be-triggered
Also, you're gonna have to change the redirect url from relative to an absolute path : https://nextjs.org/docs/messages/middleware-relative-urls
{root}/middleware.ts
import { NextResponse } from "next/server";
const signedinPages = ["/", "/playlist", "/library"];
export default function middleware(req) {
if (signedinPages.find((p) => p === req.nextUrl.pathname)) {
const token = req.cookies.JAMMLY_ACCESS_TOKEN;
if (!token) {
return NextResponse.redirect(new URL("/signin", req.url));
}
}
}
with next 13.3.1
export default function middleware(req: NextRequest) {
if (signedInPages.find((page) => page === req.nextUrl.pathname)) {
const token = req.cookies.get('TRAX_ACCESS_TOKEN')?.value;
if (!token) {
return NextResponse.rewrite(new URL('/signin', req.url))
}
}
}
I just through to the Middleware Edge section and I still cannot figure out why it's not redirecting me to the login page. I even just cloned the repo from main, updated my .env.local to my PostgreSQL I was using before, ran the migrations and can see everything in prisma studio but I still get a 401 from the fetcher.ts lib too. There is no cookies that match to token which should be right since I haven't logged in. Not sure even where to start.
I did update to
middleware.ts
and to do the response