Next.js Discord

Discord Forum

Why i should give "id" key to session.user object?

Unanswered
i_am_random_name posted this in #help-forum
Open in Discord
Hi, as a newcomer to backend development, I am currently following a tutorial. I have a question regarding the importance of the session.user.id property. In my tutorial, this property is provided with an id value, and I noticed in the official documentation that it is also mentioned as user.session.id. However, in my case, I am using the _id value from my database for each session. I'm wondering what the purpose of having both session.user.id and _id is, and why it is important.

this is the code
import NextAuth from 'next-auth';
import GoogleProvider from 'next-auth/providers/google';

import User from '@models/user';
import { connectToDB } from '@utils/database';

const handler = NextAuth({
  providers: [
    GoogleProvider({
      clientId: process.env.GOOGLE_ID,
      clientSecret: process.env.GOOGLE_CLIENT_SECRET,
    })
  ],
  callbacks: {
    async session({ session }) {
      console.log(session)
      // store the user id from MongoDB to session
      const sessionUser = await User.findOne({ email: session.user.email });
      session.user.id = sessionUser._id.toString();

      return session;
    },

.........

0 Replies