Passport: Allow sign up with name and email address? (Local Strategy)

后端 未结 6 2265
孤街浪徒
孤街浪徒 2020-12-31 01:15

Is there any way to allow a user to register on the local strategy with his password, email and name?
Every example I could find online only use name/password or email/p

6条回答
  •  囚心锁ツ
    2020-12-31 01:32

    This has actually nothing to do with passport and is pretty simple, assuming you are using body-parser. Make sure you have an input field in your form with the attribute name="name" where you register the user's name like:

    In your routing, you can access this field with req.body.name:

    passport.use('local-signup', new LocalStrategy({
        usernameField: 'email',
        passwordField: 'password',
        //are there other options?
        //emailField did not seem to do anything
        passReqToCallback: true
    },
    function(req, email, password, done) {
        //check if email not already in database
        //create new user using "email" and "password"
        //I want an additional parameter here "name"
    
        user.email = email;
        user.password = password; // Do some hashing before storing
    
        user.name = req.body.name;
    
    }));
    

    So you can add as many form input fields as you want, access them by the value of the name attribute. A Second example would be:

    
    
    
    // Access them by
    user.city = req.body.city;
    user.country = req.body.country;
    

提交回复
热议问题