我的API路线不起作用;的NodeJS

时间:2017-01-09 11:34:54

标签: javascript node.js api express server

我正在使用NodeJS开发身份验证API系统。 /Signup API端点工作正常,但/authenticate不正常。每当我调用/authenticate端点时,即使提供了有效用户,我也会获得error message: 'Could not authenticate user'; 以下是我的代码。请告诉我这里我做错了什么

var express = require("express");
var mongoose = require("mongoose");

var User = require("../models/user");

module.exports = function (router) {
    router.post('/signup', function (req,res) {
        var user = new User();
        user.local.username = req.body.username;
        user.local.email = req.body.email;
        user.local.password = req.body.password;

        if (req.body.username  == null || req.body.username == '' || req.body.email  == null || req.body.email == '' || req.body.password  == null || req.body.password == '') {
            res.json({success:false, message:'Ensure username, email and password were provided'});
        } else {
            user.save(function (err, data) {

                if (err) res.json({success:false, message:'Username or Email already exists!'});
                        //  console.log(err.errors)
                res.json({success:true, message:'New user created', data:data});
                console.log(data)
            }); 
        }
    })

    router.post('/authenticate', function (req,res) {
        User.findOne({username: req.body.username}).exec(function (err,user) {
            if(err) 
                return res.send(err);

            if (!user) {
                res.json({success:false, message: 'Could not authenticate user'});
            } else if(user){
                var validPassword = user.comparePassword(req.body.password)
                if (!validPassword) {
                    res.json({success:false, message: 'Could not authenticate password'});
                } else{
                    res.json({success:true, message: 'User authenticated'});
                }
            }
        });
    });
}

修改
用户模型:

var mongoose = require('mongoose');   
var Schema   = mongoose.Schema;   
var bcrypt   = require('bcrypt-nodejs');

// define the schema for our user model   
var userSchema = new Schema({

    local            : {
        username     : {type:String,unique:true,required:true, lowercase:true},
        email        : {type:String,unique:true,required:true, lowercase:true},
        password     : String
    },

    created_at       : {type:Date, default:Date.now},
    updated_at       : {type:Date, default:Date.now}

});

userSchema.pre('save', function(next){
    var user = this;      
    var now = new Date();     
    user.updated_at = now;    
    if(!user.created_at){         
       user.created_at = now      
    }         
    bcrypt.hash(user.local.password, null, null, function (err, hash) {
        if(err) return next(err)
        user.local.password = hash;
        next();     })  

});

// checking if password is valid   
userSchema.methods.comparePassword = function(password) {
    return bcrypt.compareSync(password, this.local.password); };

// create the model for users and expose it to our app   
module.exports = mongoose.model('User', userSchema);

1 个答案:

答案 0 :(得分:1)

刚刚看到这个错误,你在当地有你的用户名。

 router.post('/authenticate', function (req,res) {
    User.findOne({'local.username': req.body.username}).exec(function (err,user) {
        if(err) 
            return res.send(err);
        else{
            }
    });