After login in, some of the data returned does not show up on the session, it only returns the email, name. The ID however goes under the user object. Also, I have console.log(user.data) which returns the user perfectly with all those attributes.
[...nextauth].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: '[email protected]',
// 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 [email protected]
};
}
},
index.js
const { data: session } = useSession();
console.log(session);
// OUTPUT:
// expires: "2022-02-18T08:55:39.898Z"
// id: 1
// user: {name: 'admin', email: '[email protected]'}
Basically, you need to pass all the values in the callback and it will work. Passing all the token values to the 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;
},
},