I'm using Next.js middleware to redirect to the login page if there's no available token using Spotify's API,
My middleware looks like this:
import { getToken } from "next-auth/jwt";
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export async function middleware(req: NextRequest) {
// const token = await getToken({ req, secret: process.env.JWT_SECRET });
const token = await getToken({ req, secret: process.env.NEXTAUTH_SECRET });
const { pathname, origin } = req.nextUrl;
if (pathname.includes("/api/auth") || token) {
return NextResponse.next();
}
// console.log(origin);
// console.log(token);
if (!token && pathname != "/login") {
return NextResponse.redirect(new URL(`${origin}/login`));
}
}
If I comment out this section:
if (!token && pathname != "/login") {
return NextResponse.redirect(`${origin}/login`);
}
I stop getting the error but obviously, I need this line to redirect if there's no token, I tried to check for any syntax errors or any mistakes but I can't seem to find it myself, any help?
the errors i'm getting are these :
Uncaught SyntaxError: expected expression, got '<'
react-refresh.js:1
Uncaught SyntaxError: expected expression, got '<'
webpack.js:1
Uncaught SyntaxError: expected expression, got '<'
main.js:1
Uncaught SyntaxError: expected expression, got '<'
_app.js:1
Uncaught SyntaxError: expected expression, got '<'
login.js:1
Uncaught SyntaxError: expected expression, got '<'
_buildManifest.js:1
Uncaught SyntaxError: expected expression, got '<'
Looks like this is a recent issue with Next.js and hopefully, they will fix it.
Please see: https://github.com/vercel/next.js/issues/38934#issuecomment-1193083803 which seems to be the same issue as the one you are facing.
Meanwhile, please try downgrading Next.js and React.js to see if that helps.