I am currently facing an issue with deploying my Next.js project on Vercel. While the login functionality works perfectly in a development environment, I encounter difficulties when trying to sign in with credentials in Production, receiving a 401 error status.
My suspicion is that the authorize function is returning null, leading to the 401 status code. I am not using an adapter for the MongoDB database - could this be the root cause of the problem?
Below is the snippet of authorize code:
async authorize(credentials): Promise<UserAuthentication | null> {
await connectToDatabase()
// Additional logic here to authenticate the user using provided credentials
console.log('authorize function is running')
// Verify if the user exists in the database
const user = await UserModel.findOne({
username: credentials?.username,
// password: credentials?.password,
})
if (user && credentials?.password) {
console.log(credentials?.password)
const checkPassword = await compare(
credentials?.password,
user.password
)
// Incorrect password - return null
if (!checkPassword) {
console.log('Password doesnt match')
return null
}
console.log('checkPassword:', checkPassword)
}
// Success response
console.log('user', user)
if (!user) {
console.log('NO USER')
return null
}
// Return authenticated user object if all checks pass
return user
},
}),
GoogleProvider({
clientId: googleClientId,
clientSecret: googleClientSecret,
}),
],
})
}`