Error while using the callbacks option in Next-Auth
Unanswered
Santhosh Prabhakaran posted this in #help-forum
Hello guys, I'm trying to send the custom session from the Next-Auth to my client side. My
Here in callbacks, I'm trying to assign the session values with the
app/api/auth/[...nextauth]/route.js Looks like, export const authOptions = {
providers: [
CredentialsProvider({
name: "Credentials",
credentials: {
email: { label: "Email", type: "email", placeholder: "Email" },
password: {
label: "Password",
type: "password",
placeholder: "Password",
},
},
async authorize(credentials, req) {
try {
const client = await MongoClient.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
const db = client.db("test");
const user = await db
.collection("users")
.findOne({ email: credentials.email });
if (!user) throw new Error("User is not found!");
if (user) {
const checkPassword = await bcrypt.compare(
credentials.password,
user.password
);
if (!checkPassword) throw new Error("Password is incorrect!");
}
return {
id: 1,
name: user.name,
};
} catch (error) {
throw new Error(error);
}
},
}),
],
callbacks: {
async session({ session, user }) {
session.user.name = user.name;
session.user.id = user._id;
session.user.username = "user.username";
return session;
},
},
};
const handler = NextAuth(authOptions);
export { handler as POST, handler as GET };Here in callbacks, I'm trying to assign the session values with the
user that I got above from the MongoDB. But it returns null. How to use the user I got from the MongoDB to assign the values fro session in Callbacks ?20 Replies
are u sure the strategy: "database" instead of "jwt"?
Do I need to mention it ?
I'm new to Next-auth
But signin with credentials only support "jwt"
cuz i don’t see a jwt callback in ur code.
I don't get it. jwt callbacks is for token.But I'm trying to access the session
In session callback I want to send the user details which I obtained from MongoDB as you can see in the code.
jwt call back is invoked every time you get a session with options.
check my poc codes with jwt no prisma
https://github.com/tfutada/zenn-nextjs/blob/main/app/options.ts
https://github.com/tfutada/zenn-nextjs/blob/main/app/options.ts
prisma, with strategy: database version
https://github.com/tfutada/next-13-auth-prisma/blob/b3e1e4958684108a84b0b24798b0b9735796dece/app/options.ts#L63
https://github.com/tfutada/next-13-auth-prisma/blob/b3e1e4958684108a84b0b24798b0b9735796dece/app/options.ts#L63
keep in mind that session callback signature is slightly different. token or user.
session: async ({session, token, user}) => {
// When using database sessions, the User (user) object is passed as an argument.
// When using JSON Web Tokens for sessions, the JWT payload (token) is provided instead.use debugger to reveal what values are being passed, which callbacks are being called.
How can I get the user object in session callback as same as jwt callback ?
I got it. @tafutada777
I'm getting the user object in jwt's callback as I expectedthis means that strategy is 'jwt'
see the comment in my code
// When using JSON Web Tokens for sessions, the JWT payload (token) is provided instead.
We need to manipulate the user object in jwt callback and access it in the session token. right
Now I'm able to send the desired data in the session. Thank you so much for your time and help! 🫂
np. its FAQ actually.
I think I need to spend more time with Next-auth