为什么Mongoose在查询中找不到用户?

时间:2017-03-02 02:22:08

标签: javascript node.js mongodb

我一直致力于处理用户注册/登录的Nodejs + Express + Mongoose API,当用户注册时一切正常,但是当用户想要登录时说它无法找到用户。我来告诉你代码:

    module.exports.registerUser = (req, res) => {
    var user = new User();

    user.email = req.body.email;
    user.name = req.body.name;

    user.setPassword(req.body.password);

    user.save((err) => {
        var token;
        token = user.generateJwt();
        res.status(200);
        res.json({
            'token': token,
            'user': user,
        });
    });
};

当我注册时(我正在使用Postman对此进行测试)我收到此消息,显示一切正常:

{
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0ODg1MDcwNjUsImlhdCI6MTQ4ODQyMDY2NX0.GD0NehQ1EYEnOKx2OJWALpkHB8u5N_9Zjm1dcuEdl7I",
  "user": {
    "__v": 0,
    "name": "Stack Overflow",
    "email": "stackoverflow@stackoverflow.com",
    "_id": "58b77f39cf5e8d5f3ffdb4ff"
  }
}

密码被哈希,这就是JSON不包含密码的原因。

这是用户登录的代码:

module.exports.loginUser = (req, res) => {
    var password = req.body.password;
    var email = req.body.email;
    User.findOne({'email': email}, (err, user) => {
        if (err) {
            res.status(500).json(err);
        }
        if (user) {
            if (!user.validPassword(password)) {
                res.status(401).send({
                    message: 'Wrong password.'
                });
            } else {
                token = user.generateJwt();
                res.status(200);
                res.json({
                    'token': token,
                    'user': user
                });
            }
        } else {
            res.status(404).send({
                message: 'No user was found.'
            });
        }
    });
}

但每次我使用相同的密码和电子邮件点击邮递员的登录按钮时,我都会这样:

{
  "message": "No user was found."
}

这是用户架构:

'use strict'

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var crypto = require('crypto');
var jwt = require('jsonwebtoken');
var secret = require('../config/secret');

var userSchema = new Schema({
    email: {
        type: String,
        required: true,
        unique: true
    },
    name: {
        type: String,
        required: true
    },
    age: {
        type: Number,
        required: false
    },
    schoolID: {
        type: String,
        required: false,
        unique: false
    },
    university: {
        type: String,
        required: false
    },
    area: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Area'
    },
    project: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Project'
    },
    hash: String,
    salt: String
});

userSchema.methods.setPassword = (password) => {
    this.salt = crypto.randomBytes(16).toString('hex');
    this.hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64, 'sha1').toString('hex');
};

userSchema.methods.validPassword = (password) => {
    this.hash = crypto.pbkdf2Sync(password, this.salt, 1000, 64, 'sha1').toString('hex');
    return this.hash === hash;
};

userSchema.methods.generateJwt = () => {
    var expiry = new Date();
    expiry.setDate(expiry.getDate() + 1);

    return jwt.sign({
        _id: this._id,
        email: this.email,
        name: this.name,
        exp: parseInt(expiry.getTime() / 1000),
    }, secret.secret);
};

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

我无法找到解决这个问题的方法,因为我看到它的方式,一切都应该工作得很好。关于为什么这不起作用的任何想法?

更新: 这是为了检查电子邮件是否正确发送:

{
  "message": "No user was found. User: stackoverflow@stackoverflow.com password: stackoverflow"
}

0 个答案:

没有答案