I'm building a Spotify clon, and i`m trying to use middleware to check if there's a token, if there's a token the you're logged in and you can use the clon. If are not logged in or if you log out middleware should redirect you to the log in page. But it doesn't work. I've read in the docs that i need to use NextRequest, but in typescript, but in that case the JWT_SECRET gives an error. So I don't know what to do, help.
import { getToken } from "next-auth/jwt";
import { NextResponse,NextRequest } from "next/server";
export async function middleware(req) {
// Token existe si el usuario esta logeado.
const token = await getToken({ req, secret: process.env.JWT_SECRET });
const { pathname } = req.nextURL;
if (pathname.includes("/api/auth") || token) {
return NextResponse.next();
}
if (!token && pathname !== "/login") {
return NextResponse.redirect("/login");
}
}