Next.js Discord

Discord Forum

middleware

Unanswered
Galician Shepherd Dog posted this in #help-forum
Open in Discord
Avatar
Galician Shepherd DogOP
i have a problem in middleware for auth.js v5 user is logged in and its possible to check it buy auth function but , in middleware and authorizued callback the auth is null, i want to handle redirects in middleware but its not working,

authorized({ auth, request: { nextUrl } }) {
console.log(!!auth?.user, "auth");
const isLoggedIn = !!auth?.user;
console.log("log", isLoggedIn);
const isOnTopic = nextUrl.pathname.startsWith("/topics");
console.log(isOnTopic, "isOnDashboard");
if (isOnTopic) {
if (isLoggedIn) return true;
// return false; // Redirect unauthenticated users to login page
return Response.redirect(new URL("/api/auth/signin", nextUrl));
} else if (isLoggedIn) {
return Response.redirect(new URL("/topics", nextUrl));
}
return true;
},

and this middleware file
import { auth } from "./auth";
export default auth((req) => {
console.log(req.auth, "what is this ");
});

export const config = {
matcher: ["/((?!api|_next/static|_next/image|favicon.ico).*)"],
};

7 Replies

Avatar
Galician Shepherd DogOP
is it possible that its because of prisma ? since i have this error also : [auth][details]: {}
[auth][error][AdapterError]: Read more at https://errors.authjs.dev#adaptererror
[auth][cause]: Error: PrismaClient is unable to run in Vercel Edge Functions. As an alternative, try Accelerate: https://pris.ly/d/accelerate.
Avatar
Ray
yes most likely
it shows how to deal with it
Avatar
Galician Shepherd DogOP
Yes exactly in this tutorial they diden't use Prisma , i tried to do the same but i have used Prisma and i'm facing this error