繁体   English   中英

我的API路由无法正常工作; 节点JS

[英]My API Route is not working; NodeJS

我正在使用NodeJS进行身份验证API系统。 /Signup API端点正常运行,但/authenticate无效。 每次我调用/authenticate端点时,都会收到error message: 'Could not authenticate user'即使提供了有效的用户,也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);

刚刚看到该错误,即您在本地拥有用户名。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM