Currently I am returning the users email to the session as a JWT. This is working just fine. Now I am trying to return the users username with the email as a JWT. If I am to console.log the username, it is appearing in the terminal. Yet, if I am logging the session.user in my getServerSideProps function I am only able to view the email. Any tips?
export default NextAuth({
session: { jwt: true },
providers: [
CredentialsProvider({
async authorize(credentials) {
const client = await connectToDatabase();
const userCollection = client.db().collection("users");
const user = await userCollection.findOne({ email: credentials.email });
console.log(user.userName);
if (!user) {
client.close();
throw new Error("No user found!");
}
const isValid = await comparePasswords(
credentials.password,
user.password
);
if (!isValid) {
client.close();
throw new Error("Invalid password");
}
client.close();
if (user) {
return {
email: user.email,
userName: user.userName,
};
} else {
return null;
}
},
}),
],
});