• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

914
Vistas
Why did user object is undefined in NextAuth session callback?

I use NextAuth for signIn with discord provider and I need to add userID into the session object. For that I use session callback but user object is undefined. When I try to add userID to the session object, I got an error like this :

[next-auth][error][JWT_SESSION_ERROR] https://next-auth.js.org/errors#jwt_session_error Cannot read properties of undefined (reading 'id')

Indeed, it seems the user object is undefined but I don't have any solution.

export default NextAuth({
  providers: [
    DiscordProvider({
      clientId: process.env.DISCORD_CLIENT_ID,
      clientSecret: process.env.DISCORD_CLIENT_SECRET,
      authorization: { params: { scope: 'identify' } }
    })
  ],
  session: {
    jwt: true
  },
  jwt: {
    secret: process.env.JWT_SECRET
  },
  callbacks: {
    async session({session, user}) {
      session.user.id = user.id
      return session
    },
    async jwt(token) {
      return token
    }
  }
})
about 3 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can do like that in your callback :-

callbacks: {
  jwt:({token,user})=>{
    if(user){
      token.id = user.userId;
    }
    return token;
  },
  session:({session,token}) =>{
    if(token){
      session.user.id = token.id;
    }
    return session;
  }
}

and don't forget to return the user in the function which is executed just before the callback, otherwise it tell undefined.

about 3 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda