0

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");
  }
}
Iván
  • 41
  • 10
  • How is it not working? Do you get an error? Does `getToken` return the expected value? – juliomalves Sep 10 '22 at 14:23
  • Get token works, and I don't get any error. But when you are not logged in it doesn't redirect you to the login page – Iván Sep 12 '22 at 20:40

0 Answers0