-
I am getting an error in my project[auth][error] JWTSessionError: Read more at https://errors.authjs.dev#jwtsessionerror db.tsimport { PrismaClient } from "@prisma/client/edge";
const prismaClientSingleton = () => {
return new PrismaClient();
};
declare global {
var prisma: undefined | ReturnType<typeof prismaClientSingleton>;
}
const db = globalThis.prisma ?? prismaClientSingleton();
export default db;
if (process.env.NODE_ENV !== "production") globalThis.prisma = db;
auth.config.tsimport { createUser, fetchUsersWithEmail } from "@/app/_actions/users";
import { NextAuthConfig } from "next-auth";
import Google from "next-auth/providers/google";
import { PrismaAdapter } from "@auth/prisma-adapter";
import db from "./db";
export const authConfig: NextAuthConfig = {
adapter: PrismaAdapter(db),
session: { strategy: "jwt" },
providers: [Google],
pages: {
signIn: "/login",
},
callbacks: {
async session({ session, token }) {
if (token) {
session.user.name = token.name!;
session.user.email = token.email!;
session.user.image = token.picture!;
}
return session;
},
async jwt({ token, user }) {
const dbUser = await fetchUsersWithEmail(user?.email || "");
if (!dbUser) {
const res = await createUser({ name: user.name!, email: user.email!, image: user.image! });
token.id = res.id;
return token;
}
console.log("dbUser", dbUser);
return {
id: dbUser.id,
name: dbUser.name,
email: dbUser.email,
image: dbUser.image,
};
},
redirect() {
return "/";
},
},
}; auth.tsimport NextAuth from "next-auth";
import { authConfig } from "./auth.config";
export const { handlers, signIn, signOut, auth } = NextAuth(authConfig); middleware.tsimport { auth } from "@/lib/auth";
import { NextResponse } from "next/server";
export default auth(req => {
const pathname = req.nextUrl.pathname;
// manage route protection
const isAuth = req.auth;
const isLoginPage = pathname.startsWith("/login");
const sensitiveRoutes = ["/"];
const isAccessingSensitiveRoute = sensitiveRoutes.some(route => pathname.startsWith(route));
if (isLoginPage) {
if (isAuth) {
return NextResponse.redirect(new URL("/", req.url));
}
return NextResponse.next();
}
if (!isAuth && isAccessingSensitiveRoute) {
return NextResponse.redirect(new URL("/login", req.url));
}
if (pathname === "/") {
return NextResponse.redirect(new URL("/", req.url));
}
});
export const config = {
matcher: ["/", "/login"],
}; |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
did you manage to find a solution for this? Im also experiencing the same issue when initializing a middleware. Im using mongoose for object modeling |
Beta Was this translation helpful? Give feedback.
-
just clear the cookies, when you run the project on browser -> inspect element Application -> Cookies right click clear, the issue solved. that is all, I have the same issue. |
Beta Was this translation helpful? Give feedback.
Yes, I found a solution for implementing Google authentication in a Next.js 14 application using NextAuth.js 5 (beta), Prisma ORM, and MongoDB. Check out this article on medium