I am encountering an issue where I retrieve a user from the database and store it in a variable called result. However, I noticed that the result object does not contain the password key, resulting in the value of result.password being undefined. I am unsure why this is happening and would appreciate any help or insights on this matter. I have seen others on YouTube perform similar actions, but their users do come with the password key.
import CredentialsProvider from "next-auth/providers/credentials"
import dbConnect from '../../../lib/dbConnect';
import User from '../../../models/User';
import { compare } from 'bcryptjs';
export default NextAuth({
providers: [
CredentialsProvider({
name: 'Credentials',
async authorize(credentials, req) {
await dbConnect()
console.log(credentials)
//check user existence
const result = await User.findOne({email: credentials.email})
if(!result){
throw new Error('No user Found With Email Please Sign Up!')
}
console.log(result) /* the output give me the user back but without the
password so result.password is undefind
{
_id: new ObjectId("63c6cf0eeed325fcb7d46dbd"),
name: 'test user',
email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d2b7aab3bfa2beb792b5bfb3bbbefcb1bdbf">[email protected]</a>',
provider: false,
isAdmin: false,
createdAt: 2023-01-17T16:38:38.133Z,
updatedAt: 2023-01-17T16:38:38.133Z,
__v: 0
}
even though i check the database and i found the user there with a
password
*/
//compare password
const checkPassword = await compare(credentials.password, result.password)
if(!checkPassword || result.email !== credentials.email){
throw new Error("Email or Password dosen't match")
}
return result
}
})
],
database: process.env.DB_URL
})