Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

276
Visualizações
Session in Next.js not returning correct values

After login, some of the data returned does not appear in the session, it only returns the email and name. However, the ID goes below the user object. Also, I have console.log( user.data ) which returns the user perfectly with all those attributes.

[...next].js

 async authorize(credentials) { const user = await axios.post("http://localhost:4000/api/auth/login", { email: credentials.username, password: credentials.password, }); console.log(user.data); //{ // user: { // id: 1, // name: 'admin', // surname: 'surname admin', // email: 'admin@email.com', // avatar: null, // role: '1', // dob: '2020-01-01T00:00:00.000Z', // description: null, // cv: null, // createdAt: '2022-01-07T00:03:09.000Z', // updatedAt: '2022-01-07T00:03:09.000Z' // } //} // if (user.data.message === "User not found") { return null; } if (user.data.user) { return { id: user.data.user.id, //suppose to be 1 name: user.data.user.name, //suppose to be admin surname: user.data.user.surname, //suppose to be surname email: user.data.user.email, //suppose to be admin@email.com }; } },

index.js

 const { data: session } = useSession(); console.log(session); // OUTPUT: // expires: "2022-02-18T08:55:39.898Z" // id: 1 // user: {name: 'admin', email: 'admin@email.com'}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Try adding something like this inside the NextAuth option

 callbacks: { async session({ session, token }) { session.user = token.user; return session; }, async jwt({ token, user }) { if (user) { token.user = user; } return token; }, },
over 4 years ago · Santiago Trujillo Relatório

0

Basically you need to pass all the values in the callback and it will work. Pass all token values to session object

 callbacks: { jwt: ({ token, user }) => { if (user) { token.id = user.id; token.name = user.name; token.surname = user.surname; token.email = user.email; } return token; }, session: ({ session, token }) => { if (token) { session.id = token.id; session.name = token.name; session.surname = token.surname; session.email = token.email; } return session; },
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda