Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

274
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!