I am using mongoDb to create a login page (That's not a official login)
For the login system I am using, express, session-express and passport.
There is a file for passport-config to get the information sent by the login form and after comapre the info with the daabase.
file: 'passport-config.js'
import LocalStrategy from 'passport-local'
LocalStrategy.Strategy
import bcrypt from 'bcrypt' // to encrypt the password
function initialize(passport, getUserByEmail, getUserById) {
const authenticateUser = async (email, password, done) => {
const user = getUserByEmail(email) // calls the 'getUserByEmail()' and it uses the email taken by the login form
// the rest of the code...
}
passport.use(new LocalStrategy({ usernameField: 'email' }, authenticateUser)) // calls authenticateUser sending the info from the form
}
export default initialize
The file 'passport-config.js' exports the function, then I import it in server.js. As parameters I use (passport, function to find the user by email, function to find the user by id)
the server.js
import initializePassaport from '../passport-config.js' // import the function from passport-config.js
import passport from 'passport'
import users from "../models/User.js"; // get the model Schema from the users database
initializePassaport(
passport,
email => users.findOne({"email": email}, {}, (err, user) => user) // get the email that was sent as a parameter of the getUserByEmail function inside the passport-config.js file
id => users.findById(id, (err, user) => {email = user} => user) // same thing, but with id
)
//more code...
My problem is when I send the parameters in 'server.js', the expected value was to find the user by emai. But instead, it sends a others values. The problem is that Apparently the value sent by the find() in mongoDb can't be outside the function.
I put a console . log: "email => users.findOne({"email": email}, {}, (err, user) => {console.log(user})" and it return the right value in the console. But it doesn't send the right value to the funciton
I've already tried to put a return. But It also doesn't work.
And I tried to research how to get this value, but I didn't find anything for this problem
This code has worked before using just a common array without a real database. Like:
const users = [ // this is an exmaple
{
"id": '167252944716',
"username": 'adm',
"email": 'adm@adm',
"password": '$2b$12$G/EwhnXj5P/y1NGTb5Sq4.OTY5m.BMferVHVJ27AtZGn8vt6qDsvi' //encrypted
}
]
initializePassaport(
passport,
email => users.find(user => user.email === email),
id => users.find(user => user.id === id)
)
I don't Know what I can I do to send this info to the passport-config.js, is there a way to fix it? if it's not clear please let me know to improve it, thank you!
CodePudding user response:
findOne and findById are async functions so it would return a promise, you need to resolve it by using await keyword or .then() method try using something like this:
const authenticateUser = async (email, password, done) => {
try {
const user = await getUserByEmail(email);
const isMatch = await bcrypt.compare(password, user.password);
if (isMatch) {
return done(null, user);
} else {
return done(null, false, { message: 'Password incorrect' });
}
} catch (error) {
return done(error);
}
};