[next-auth][error][JWT_SESSION_ERROR], how to fix this error in next-auth #8957
Unanswered
IsharaWeerasinghege
asked this question in
Help
Replies: 2 comments 1 reply
-
Facing the same exact issue, it was working fine a day back and today its throwing error. |
Beta Was this translation helpful? Give feedback.
0 replies
-
Got the same error. This happens when your database is not connected, In my case , it got fixed when i add "await connectMongoDB();" just before calling "await User.findOne()" example :
//part of callback function in auth.ts
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary
i am using next-auth for myy nextjs web application. in local development my application are working fine but the bellow error thrown in production build. how to fix this issues. i will try different methods for fixing that issues, but still cant fixed this issue.
[next-auth][error][JWT_SESSION_ERROR] https://next-auth.js.org/errors#jwt_session_error Operation
users.findOne()buffering timed out after 10000ms { message: 'Operation
users.findOne()buffering timed out after 10000ms', stack: 'MongooseError: Operation
users.findOne()buffering timed out after 10000ms\n' + ' at Timeout.<anonymous> (G:\\Next project\\videoclay\\node_modules\\mongoose\\lib\\drivers\\node-mongodb-native\\collection.js:186:23)\n' + ' at listOnTimeout (node:internal/timers:564:17)\n' + ' at process.processTimers (node:internal/timers:507:7)', name: 'MongooseError' }
mongo database connection.
db.js
`
import mongoose from 'mongoose';
const connection = {};
mongoose.set('strictQuery', true);
async function connectDb() {
if (connection.isConnected){
console.log('Already connected to the database');
return;
}
}
async function disconnectDb() {
if (connection.isConnected) {
if (process.env.NODE_ENV === 'production') {
await mongoose.disconnect();
connection.isConnected = false;
} else {
console.log('Not disconnected');
}
}
}
const db = { connectDb, disconnectDb };
export default db;
`
mongo database connation for next-auth
mongodb.js
`
import {MongoClient} from "mongodb";
const uri = process.env.MONGODB_URL;
const options = {
useUnifiedTopology: true,
// useNewUrlParser: true,
};
let client;
let clientPromise;
if (!process.env.MONGODB_URL) {
throw new Error("Please add your Mongo URI to .env.local");
}
if (process.env.NODE_ENV === "development") {
if (!global._mongoClientPromise) {
client = new MongoClient(uri, options);
global._mongoClientPromise = client.connect();
}
clientPromise = global._mongoClientPromise;
} else {
client = new MongoClient(uri, options);
clientPromise = client.connect();
}
export default clientPromise;
`
[next-auth].js
`
import NextAuth from "next-auth";
import FacebookProvider from "next-auth/providers/facebook";
import GoogleProvider from "next-auth/providers/google";
import CredentialsProvider from "next-auth/providers/credentials";
import User from "../../../models/User";
import clientPromise from "./lib/mongodb";
import bcrypt from "bcrypt";
import db from "../../../utils/db";
import {MongoDBAdapter} from "@auth/mongodb-adapter";
export default NextAuth({
adapter: MongoDBAdapter(clientPromise),
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {
username: { label: "Username", type: "text", placeholder: "jsmith" },
password: { label: "Password", type: "password" },
},
async authorize(credentials, req) {
const email = credentials.email;
const password = credentials.password;
await db.connectDb();
const user = await User.findOne({ email });
if (user) {
return SignInUser({ password, user });
} else {
throw new Error("This email does not exist.");
}
},
}),
FacebookProvider({
clientId: process.env.FACEBOOK_ID,
clientSecret: process.env.FACEBOOK_SECRET,
}),
GoogleProvider({
clientId: process.env.GOOGLE_ID,
clientSecret: process.env.GOOGLE_SECRET,
}),
],
callbacks: {
async session({ session, token }) {
const user = await User.findById(token.sub);
session.user.id = token.sub || user._id.toSting();
session.user.role = user.role || "user";
token.role = user.role || "user";
});
const SignInUser = async ({ password, user }) => {
if (!user.password) {
throw new Error("Please enter your password.");
}
const testPassword = await bcrypt.compare(password, user.password);
if (!testPassword) {
throw new Error("Email or password is wrong!");
}
return user;
};
`
Additional information
No response
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions