NextAuth signin callback issue
Unanswered
Cape lion posted this in #help-forum
Cape lionOP
I am making an e-commerce website, i have a postgres database and using nextauth for user login, what i wanna implement is that when a user logs in cart is updated with cart items obtained form user database entry, now i do have cart items as a session parameter, but how can i update cart state variable using it? As i only need to update cart once from database cart value (when user signs in) i am thinking i can do that by using callback function of signin, bit in that call back function i won't have state variable available to make changes to it, how can i accomplish this? Do i have to create some API that update state variable inside callback function of signIn?
1 Reply
Toyger
you can use session callback
with something like that, but also add additional conditions like some flag if cart was fetched and save it to same session, so you'll fetch only single time.
with something like that, but also add additional conditions like some flag if cart was fetched and save it to same session, so you'll fetch only single time.
callbacks: {
session: async (session) => {
if (!session) return;
const client = await connectToDatabase();
const usersCollection = client.db().collection('users');
const userData = await usersCollection.findOne({
email: session.user.email,
});