Next.js Discord

Discord Forum

An issue with nextAuth session object | credentialsProvider

Unanswered
Naeemgg posted this in #help-forum
Open in Discord
I want to include userId in the session object from both useSession and getServerSession for some data fetching purposes,
//authOptions.ts
import { NextAuthOptions } from "next-auth";
import CredentialsProvider from "next-auth/providers/credentials";
import bcrypt from "bcryptjs";
import prisma from "@/prisma/client";

export  const authOptions:NextAuthOptions = {
    providers: [
      CredentialsProvider({
        name: "credentials",
        credentials: {
          email: {
            label: "email",
            type: "email",
            placeholder: "example@example.com",
          },
          password: {
            label: "password",
            type: "password",
            placeholder: "******",
          },
        },
        async authorize(credentials) {
          const { email, password } = credentials as {email:string, password:string};
  
          const user = await prisma.user.findUnique({ where: { email } });
          if (!user) {
            throw new Error("Invalid email or password");
          }
          const isPasswordMatched = await bcrypt.compare(password, user.password);
          if (!isPasswordMatched) {
            throw new Error("Invalid email or password");
          }
          return user;
        },
      }),
    ],
    pages: {
      signIn: "/login",
    },
    secret: process.env.NEXTAUTH_SECRET,
  }

29 Replies

as of now I'm only getting name & email in session object
user
: {name: 'ez', email: 'joomla@dramp.com'}
@Naeemgg user : {name: 'ez', email: 'joomla@dramp.com'}
could you show your prisma schema?
yes sure
generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model User {
  id        String     @id @default(cuid())
  email     String     @unique
  name      String
  password  String
  createdAt DateTime   @default(now())
  updatedAt DateTime   @updatedAt
}
I'd like to include this id into session object
@Naeemgg I'd like to include this id into session object
how did you include it?
still trying
couldn't do it
I was thinking to add it with update function which comes with useSession
@Naeemgg I was thinking to add it with `update` function which comes with `useSession`
try adding this to authOption
callbacks: {
  async session({ session, token, user }) {
    if (user) session.user.id = user.id
    return session
  }
}
let me check it real quick
{
user: {
name: 'ez',
email: 'joomla@dramp.com',
image: undefined
}
}
@Ray try adding this to authOption ts callbacks: { async session({ session, token, user }) { if (user) session.user.id = user.id return session } }
add a log here
callbacks: {
  async session({ session, token, user }) {
    console.log({token,user})
    if (user) session.user.id = user.id
    return session
  }
}
here I'm getting id in sub
@Naeemgg Click to see attachment
from which?
in token
{
token: {
name: 'ez',
email: 'joomla@dramp.com',
sub: 'clsa0mo4q000140mva1e9w2cs',
iat: 1707228406,
exp: 1709820406,
jti: 'da619f65-71a0-44f3-9cfc-7627a80c64ef'
},
user: undefined
}
@Naeemgg in token
try this
callbacks: {
  async session({ session, token, user }) {
    if (token) session.user.id = token.id
    return session
  }
}
this is the whole object
@Ray try this ts callbacks: { async session({ session, token, user }) { if (token) session.user.id = token.id return session } }
are you using useContext somewhere?
@Ray are you using `useContext` somewhere?
Yes I'm using it
for times sake I did a dirty fix which is sending email from session to backend and fetching its id
But if I could get id in session that will save some time