passportjs用户对象不返回比较密码的密码

时间:2016-03-29 11:59:26

标签: node.js mongodb express mongoose passport.js

我有一个问题,我不知道用户存储对象为什么不在model / user.js中的validatePassword函数中返回密码。我按照passportjs官方文档中描述的所有步骤进行操作。

我使用了本地护照来登录。当我比较电子邮件它总是比较,但当我尝试执行验证密码并使用this.password或作为参数时,它总是空白,这就是为什么我的密码不比较。

我收到了所有用户架构信息,但我没有在用户对象中获取密码,所以我无法比较它。

有人能告诉我怎么能摆脱这个问题?

  

控制台日志

    root@wk11:/var/www/html/mytripmean/trunk# nodejs server.js
    Mytrip is listening on port 1000
    MongoDB connection successful
    ---- User Information ----
    myemail@gmail.com
    Password@@123
    { message: 'Incorrect password.' }
    not user:
    false
    [Error: Illegal arguments: string, undefined]

    /var/www/html/mytripmean/trunk/app/data/models/user.js:105
                throw err;
                      ^
    Error: Illegal arguments: string, undefined
        at Error (<anonymous>)
        at Object.bcrypt.compare (/var/www/html/mytripmean/trunk/node_modules/bcryptjs/dist/bcrypt.js:250:42)
        at model.userSchema.methods.validPassword (/var/www/html/mytripmean/trunk/app/data/models/user.js:102:12)
        at Query.<anonymous> (/var/www/html/mytripmean/trunk/app/data/routes/user.js:222:27)
        at /var/www/html/mytripmean/trunk/node_modules/kareem/index.js:177:19
        at /var/www/html/mytripmean/trunk/node_modules/kareem/index.js:109:16
        at process._tickCallback (node.js:448:13)
  

模型/ user.js的

    var mongoose = require('mongoose')
            , Schema = mongoose.Schema
            , ObjectId = Schema.ObjectId
            , randtoken = require('rand-token')
            , bcrypt = require("bcryptjs");

    var userSchema = new Schema({
    //  _Id: objectId,
        social_id: {
            type: String, //(Social id of facebook/twitter)
            required: false,
            unique: false
        },
        social_media: {
            type: String, //(facebook/twitter)
            required: false,
            unique: false
        },
        link_id: {
            type: String, //will be dispalying as user reference in url
            required: true,
            unique: true
        },
        nick_name: {
            type: String, // Unique Nickname for signup
            required: true,
            unique: true
        },
        email: {
            type: String, // Unqiue Email for signup
            required: true,
            unique: true
        },
        password: {
            type: String, // Password
            required: true,
            select: false
        },
        user_type: {
            type: Number, // 1: SuperAdmin, 2: Admin, 3: SiteUser, 4: Restaurant
            required: true
        }, //reason_to_close: String, // Close Account
        is_active: {
            type: Number, // -1: pending to activation, 0: inactive, 1: active, 
            required: true
        },
        is_close: {
            type: Number, // -1: pending to close/Undecided, 0: closed , 1: open/ not close, 
            required: true
        },
        is_online: {
            type: Number, // 0: Offline, 1: Online
            required: true
        },
        created_at: {
            type: Date,
            default: Date.now
        }, // Registration date
        updated_at: {
            type: Date, // Registration activation date / user update date
            default: Date.now
        }
    }, {collection: 'user'});

    // Password verification
    userSchema.methods.validPassword = function (candidatePassword, callback) {
        bcrypt.compare(candidatePassword, this.password, function (err, isMatch) {
            console.log(err);
            if (err) {
                throw err;
            }
            callback(null, isMatch);
        });
    };

    var User = module.exports = mongoose.model("User", userSchema);

    module.exports.checkEmail = function (callback) {
        return this.model('User').count({email: this.email}, callback);
    };

    module.exports.validateEmailOrNickname = function (username, callback) {
        var orCondition = [{nick_name: username}, {email: username}];
        //return this.model("user").findOne().or(orCondition);
        return this.model("User").find({$or: orCondition}, callback);
    };

    module.exports.getUserById = function (id) {
        User.findById(id, callback);
    };

    module.exports.createUser = function (user, callback) {
        bcrypt.genSalt(10, function (err, salt) {
            bcrypt.hash(user.password, salt, function (err, hash) {
                user.password = hash;
                user.save(callback);
            });
        });
    };
  

路由/ user.js的

    var express = require('express');
    var router = express.Router();
    var bcrypt = require("bcryptjs")
    var User = require('../models/user');
    var UserProfile = require('../models/userProfile');
    var UserSignupToken = require('../models/userSignupToken.js');
    var IpLogger = require('../models/ipLogger.js');
    var passport = require("passport");
    var localStrategy = require("passport-local"), Startegy;

    router
            .route('/api/user/register')
            .post(
                    function (req, res, next) {

                        var user_, userData_;
                        userData_ = {
                            link_id: req.body.manLinkId,
                            nick_name: req.body.txtNickname,
                            email: req.body.txtEmail,
                            password: req.body.manPassword,
                            user_type: req.body.manUserType,
                            is_active: req.body.manIsActive,
                            is_close: req.body.manIsClose,
                            is_online: req.body.manIsOnline
                        };

                        user_ = new User(userData_);

                        user_.validate(function (err) {
                            if (err) {

                            } else {
                                //check recaptch is validate or not
                                var request = require('request');
                                request
                                        .post({
                                            url: 'http://www.google.com/recaptcha/api/verify',
                                            form: {
                                                privatekey: process.env.RECAPTCHA_PRIVATE_KEY,
                                                remoteip: req.connection.remoteAddress,
                                                challenge: req.body.captcha.challenge,
                                                response: req.body.captcha.response
                                            }
                                        }, function (err, httpResponse, body) {
                                            if (body.match(/false/) === null) {
                                                //Recaptcha validated
                                                User.createUser(user_, function (err, data) {
                                                    if (err) {
                                                        console.log("stpe 1:");
                                                        console.log(err);
                                                        res.json({status: 0, message: 'User having an error on stage 1'});
                                                    } else {
                                                        res.locals.user = data;
                                                        //res.json({error:1, message: 'User saved'});
                                                        next();
                                                    }
                                                });
                                                //res.json({ "captchaError": true });
                                            } else {
                                                res.json({"captchaError": false});
                                            }
                                        });
                            }
                        });
                    },
                    function (req, res, next) {
                        var userProfileData_, userProfile_;
                        userProfileData_ = {
                            user_id: res.locals.user.id,
                            link_id: res.locals.user.link_id,
                            full_name: req.body.txtFullname,
                            is_active: -1
                        };
                        userProfile_ = new UserProfile(userProfileData_);
                        userProfile_.save(function (err, data) {
                            if (err) {
                                console.log("stpe 2:");
                                console.log(err);
                                res.json({status: 0, message: 'User having an error on stage 2'});
                            } else {
                                //res.json({error:1, message: 'User profile generated'});
                                next();
                            }
                        });
                    },
                    function (req, res, next) {
                        var userSignupTokenData_, userSignupToken_;
                        userSignupTokenData_ = {
                            user_id: res.locals.user.id,
                            link_id: res.locals.user.link_id,
                            is_active: -1
                        };
                        userSignupToken_ = new UserSignupToken(userSignupTokenData_);
                        userSignupToken_.save(function (err, data) {
                            if (err) {
                                console.log("stpe 3:");
                                console.log(err);
                                res.json({status: 0, message: 'User having an error on stage 3'});
                            } else {
                                //res.json({error:1, message: 'User signup token generated'});
                                next();
                            }
                        });
                    },
                    function (req, res, next) {
                        var ipLoggerData_, ipLogger_, client_IP;
                        ipLoggerData_ = {
                            user_id: res.locals.user.id,
                            link_id: res.locals.user.link_id,
                            client_ip: req.ip,
                            activity: "signup"
                        };
                        ipLogger_ = new IpLogger(ipLoggerData_);
                        ipLogger_.save(function (err, data) {
                            if (err) {
                                console.log("stpe 4:");
                                console.log(err);
                                res.json({status: 0, message: 'User having an error on stage 4'});
                            } else {
                                res.json({status: 1, message: 'user saved'});
                            }
                        });
                    }
            );
    //Check unique validation
    router
            .route('/api/user/authenticate')
            .post(
                    function (req, res, next) {
                        console.log("---- User Information ----");
                        console.log(req.body.txtSigninEmail);
                        console.log(req.body.txtSigninPassword);

                        passport.authenticate('local', function (err, user, info) {
                            console.log(info);
                            if (err) {
                                console.log(err);
                                return next(err);
                            }
                            if (!user) {
                                console.log("not user:");
                                console.log(user);
                                return res.status(401).json({
                                    err: info
                                });
                            }
                            req.login(user, function (err) {
                                if (err) {
                                    return res.status(500).json({
                                        err: 'could not login user'
                                    });
                                }
                                res.status(200).json({
                                    status: 'login successful'
                                });
                            });
                        })(req, res, next);
                    });
    router
            .route('/api/user/checkEmail')
            .post(
                    function (req, res) {
                        User.count({email: req.body.txtSigninPassword}, function (err, user) {
                            if (err) {
                                // console.log("error false");
                                res.json(false);
                            } else {
                                // console.log("data");
                                // console.log(user);
                                res.json({"status": user > 0 ? false : true});
                            }
                        });
                    });


    passport.serializeUser(function (user, done) {
        done(null, user.id);
    });

    passport.deserializeUser(function (id, done) {
        User.findById(id, function (err, user) {
            done(err, user);
        });
    });


    passport.use('local', new localStrategy(
            {
                usernameField: 'txtSigninEmail',
                passwordField: 'txtSigninPassword'
            },
            function (username, password, done) {
                User.findOne({email: username}, function (err, user) {
                    if (err) {
                        return done(err);
                    }
                    if (!user) {
                        return done(null, false, {message: 'Incorrect username.'});
                    }
                    if (!user.validPassword(password)) {
                        return done(null, false, {message: 'Incorrect password.'});
                    }
                    return done(null, user);
                });
            }
    ));
    module.exports = router;

1 个答案:

答案 0 :(得分:1)

经过2个小时的努力,我找到了回答我的问题。在我的用户模型密码字段中,我设置了属性“select:false”,因为我始终将密码设置为空白。

旧版:

var userSchema = new Schema({
      password: {
            type: String, // Password
            required: true,
            select: false
        },
}

重新设置后选择:true它可以正常工作。

更新:

var userSchema = new Schema({
      password: {
            type: String, // Password
            required: true,
            select: true
        },
}