I have the following code that connects to my database to authenticate the connection using NextAuth, in which case it returns an object (usr) that contains the id of the user logging in.
import NextAuth from 'next-auth'
import CredentialsProvider from "next-auth/providers/credentials";
import bcrypt from 'bcryptjs'
export default NextAuth({
session: {
jwt: true
},
providers: [
CredentialsProvider({
name: 'data',
credentials: {
username: { label: "Username", type: "text" },
password: { label: "Senha", type: "password" }
},
async authorize(credentials) {
const usr = await db('...')
.select('...', '...', '...')
.where({ usr_login : credentials.username })
.first();
if (!usr) { // Database connection failed or user doesn't exist
return null
} else {
// Verifying the password
if (bcrypt.compareSync(credentials.password, usr.password)) {
const ret = {
id: usr.usr_login,
}
console.debug('Authorize : ', ret);
return ret;
} else {
return null;
}
}
}
})
],
callbacks: {
jwt: async (token, user, account, profile, isNewUser) => {
console.debug('JWT : ', token, user, account, profile, isNewUser);
if(user) {
token.user = user;
}
console.debug('JWT : ', token, user, account, profile, isNewUser);
return Promise.resolve(token);
},
session: async (session, user) => {
if (session && user.user) {
session.userId = user.user.id;
return Promise.resolve(session);
} else {
// Handle the case where session or user is undefined
console.error('Session or user is undefined', { session, user });
return Promise.resolve(session);
}
},
},
pages: {
signIn: '/login'
}
})
The output from console.debug('Authorize : ', ret) shows that the object is indeed created and contains an ID, which means the database connection isn’t the issue. When it enters the JWT callback function, the first console.debug('JWT : ', token, user, account, profile, isNewUser); shows that it received the correct information, but when it tries to assign it on token.user = user, it seems to be undefined and the output I get from the logs is:
JWT : {
token: {
name: undefined,
email: undefined,
picture: undefined,
sub: 'john'
},
user: { id: 'john' },
account: {
providerAccountId: 'john',
type: 'credentials',
provider: 'credentials'
},
isNewUser: false,
trigger: 'signIn'
} undefined undefined undefined undefined
JWT : {
token: {
name: undefined,
email: undefined,
picture: undefined,
sub: 'john'
},
user: undefined,
account: {
providerAccountId: 'john',
type: 'credentials',
provider: 'credentials'
},
isNewUser: false,
trigger: 'signIn'
} undefined undefined undefined undefined
Also, when I try to debug the user alone, it also returns undefined. I thought it might be an issue with the scope of the variable, but I couldn’t see where it might be from since it’s the only user variable defined. Another possibility would be a race condition, but I don’t see how that would crash it, since it was actually defined and should be entering the if(user) {} clause, but apparently it doesn’t. I don’t know why just before using the variable it is defined, but when I try to assign it it vanishes.
I appreciate any help you can provide. Thanks 🙂

