Answer the question
In order to leave comments, you need to log in
How to setup strategy in passport.js (data fields)?
Hi all! The next question!
I have a strategy set up in passport.js, it is responsible for registration, accepts the data of the usernameField: 'email' and passwordField: 'password' fields, and checks that if the email is so busy, then it does not register a new user, if it is free, it registers (banal check for email ). If the email is correct, a new user is registered and the password and email data are added to the database table.
I also needed to transfer other data from the registration form to the database, and then problems started.
First, I added the AGE data to the request to create a row in the table and I get the answer that age is not defined
const user = await User.create({
email,
password,
age
});
passport.use('signup', new LocalStrategy(
{
usernameField: 'email',
passwordField: 'password',
ageField: 'age'
},
async (email, password, age, done) =>.....
async (email, password, done, age) =>.....
passport.use('signup', new LocalStrategy(
{
usernameField: 'email',
passwordField: 'password',
ageField: 'age'
},
async (email, password, done, age) => {
try {
const findUser = await User.findOne({
where: { email }
});
if (findUser) {
done(null, false, {
message: 'Email already taken'
});
return;
}
const user = await User.create({
email,
password,
age
});
done(null, user);
} catch (error) {
done(error);
}
}
));
Answer the question
In order to leave comments, you need to log in
Instead of ageField, you need to specify passReqToCallback: true
Then callback will be the first argument to take req.
async(req, email, password, done) ... and pull out everything you need from req.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question