我如何使用passport

我如何使用passport

本文介绍了我如何使用passport-local.js 存储其他表单字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在处理 node+passport.js 身份验证.我制作了一个简单的登录/注册应用程序.它工作正常,但它只存储用户名和密码.

I am working on a node+passport.js authentication. I make a simple login/signup app. It's working fine but, it stores only username and password.

如何通过具有有效登录护照身份验证的 signup.html 页面将其他表单字段(如电话号码、电子邮件、爱好、性别)存储到数据库中?任何人都可以解决这个问题,以便我可以将所有字段存储在数据库中....

How can I store the other Form Fields like Phone number, email, hobbies, gender into database through a signup.html page with working login passport authentication? Can anybody have solution for that so I can store all the fields in the database....

//my schema is :--
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var userSchema = mongoose.Schema({
    local            : {
        username     : String,
        gender       : String,
        phone        : String,
        email        : String,
        password     : String
    }
 });
userSchema.methods.generateHash = function(password) {
    return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};
userSchema.methods.validPassword = function(password) {
    return bcrypt.compareSync(password, this.local.password);
};


var User = mongoose.model('user', userSchema);

module.exports = User;

在这段代码中,我使用了电子邮件、用户名、密码、性别电话的架构,并在 signup.html 页面中给出了字段.但它只存储用户名和密码字段......

In this code I use schema of email, username, password, gender phone and also given fields in signup.html page. but it stores only username and password fields only.........

推荐答案

打开passport.js文件(一般在config文件夹内)

Open passport.js file ( gernally inside config folder)

找到这行代码.

    passport.use('local-signup', new LocalStrategy({   // 'login-signup' is optional here
    usernameField : 'email',
    passwordField : 'password',
    passReqToCallback : true },function(req, email, password, done) {
   var gender = req.body.gender;
  var username = req.body.username;
  var phone = req.body.phone;
 // Now you can access gender username and phone

}));

这篇关于我如何使用passport-local.js 存储其他表单字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 23:28