A
A
Alexander Koshelev2018-09-11 21:38:30
Node.js
Alexander Koshelev, 2018-09-11 21:38:30

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
            });

Okay, no problem, I forgot to define age, I define this table column in async
passport.use('signup', new LocalStrategy(
    {
        usernameField: 'email',
        passwordField: 'password',
        ageField: 'age'
    },
    async (email, password, age, done) =>.....

In this situation, the log writes to me that done is not function
Okay, I changed places
async (email, password, done, age) =>.....
Now everything is fine, no errors, but the data of the age field is not added
How to be here ?? Please help
PS here is the full code of this strategy.
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

1 answer(s)
D
Dmitry Alekseev, 2018-09-11
@Xandr24

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 question

Ask a Question

731 491 924 answers to any question