Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 节点JS:登录不工作_Javascript_Node.js_Mongodb_Express - Fatal编程技术网

Javascript 节点JS:登录不工作

Javascript 节点JS:登录不工作,javascript,node.js,mongodb,express,Javascript,Node.js,Mongodb,Express,我在节点顶部使用了带有.hbs模板的Express。我正在使用passport验证特定用户。我在MongoDB中使用的数据库 以下是我的注册路线: var express = require('express'); var router = express.Router(); var passport = require('passport'); var userServices = require('../services/user-services'); router.get('/', f

我在节点顶部使用了带有.hbs模板的Express。我正在使用passport验证特定用户。我在MongoDB中使用的数据库

以下是我的注册路线:

var express = require('express');
var router = express.Router();
var passport = require('passport');
var userServices = require('../services/user-services');

router.get('/', function(req, res, next) {

    var vm = {
    title: 'Join this web',
    };
  res.render('signup', vm);
});

router.post('/', function(req, res, next) {
  userServices.addUser(req.body, function(err){
    if(err){
    var vm = {
      title: 'Create an account',
      input: req.body,
      error: err
    };
    delete vm.input.password;
    return res.render('signup', vm);
  }
      req.login(req.body, function(err) {
      res.redirect('/profile');
    });
   });
});
router.post('/login', passport.authenticate('local'), function(req, res, next){
  res.redirect('/profile');
});

module.exports = router;
以下是我的登录路径:

var express = require('express');
var router = express.Router();
router.get('/', function(req, res, next) {
  if (req.user) {
    return res.redirect('/profile');
  }
  res.render('login', { title: 'Login' });
});

module.exports = router;
我已在passport congig文件中定义了我的passport的配置:

module.exports=function(){
    var passport = require('passport');
    var passportLocal = require('passport-local');
    var userServices = require('../services/user-services');

    passport.use(new passportLocal.Strategy({usernameField: 'email'}, function(email, password, next) {
        userServices.findUser(email, function(err, user){
            if(err){
                return next(err);
            }
            if(!user||user.password!==password){
                return next(null, null);
            }
            next(null, user);
        });
    }));

    passport.serializeUser(function(user, next){
        next(null, user.email);
    });

    passport.deserializeUser(function(user, next){
        userServices.findUser(email, function(err, user){
            next(err, user);
        });
    });
};
另外,这是我的app.js,带有护照相关代码:

var passportConfig = require('./auth/passport-config');
passportConfig();

var app = express();

app.use(expressSession({
    secret:'trawel man',
    saveUninitialized: false,
    resave: false
}));

app.use(passport.initialize());
app.use(passport.session());
以下是我收到的错误的堆栈跟踪:

Error: Not Found

at C:\Users\James\MEAN\app.js:56:15
at Layer.handle [as handle_request] (C:\Users\James\MEAN\node_modules\express\lib\router\layer.js:82:5)
at trim_prefix (C:\Users\James\MEAN\node_modules\express\lib\router\index.js:302:13)
at C:\Users\James\MEAN\node_modules\express\lib\router\index.js:270:7
at Function.proto.process_params (C:\Users\James\MEAN\node_modules\express\lib\router\index.js:321:12)
at next (C:\Users\James\MEAN\node_modules\express\lib\router\index.js:261:10)
at C:\Users\James\MEAN\node_modules\express\lib\router\index.js:603:15
at next (C:\Users\James\MEAN\node_modules\express\lib\router\index.js:246:14)
at Function.proto.handle (C:\Users\James\MEAN\node_modules\express\lib\router\index.js:166:3)
at router (C:\Users\James\MEAN\node_modules\express\lib\router\index.js:35:12)
at Layer.handle [as handle_request] (C:\Users\James\MEAN\node_modules\express\lib\router\layer.js:82:5)
at trim_prefix (C:\Users\James\MEAN\node_modules\express\lib\router\index.js:302:13)
at C:\Users\James\MEAN\node_modules\express\lib\router\index.js:270:7
at Function.proto.process_params (C:\Users\James\MEAN\node_modules\express\lib\router\index.js:321:12)
at next (C:\Users\James\MEAN\node_modules\express\lib\router\index.js:261:10)
at C:\Users\James\MEAN\node_modules\express\lib\router\index.js:603:15
这是我在服务器控制台上得到的:

POST/login 404 32.453 ms-2847


我不知道为什么这不起作用。我不熟悉Node。有人帮帮我。

您错过了
/profile
的路由,这就是调用您的最终“未找到”路由的原因

添加带有

router.get('/profile', function (req, res, next) {
    var vm = { name : req.user ? req.user.name:null };
    res.render('profile', vm); 
})
这个错误不会被抛出

编辑:

似乎您在尝试登录时出错,而不是之后。这是因为您登录后的路径位于错误的文件中。在登录路径文件中,添加以下内容:

router.post('/', passport.authenticate('local'), function(req, res, next){
  res.redirect('/profile');
});
(确保导入依赖项)

然后从注册路由中删除
router.post('/login'…

// process the login form
router.post('/login', passport.authenticate('local-login', {
    successRedirect: '/',       // redirect to the home page
    failureRedirect: '/login', // redirect back to the login page if there is an error
    failureFlash: true          // allow flash messages
}));
这是您应该使用的passport代码:(您需要添加您的用户模式路径)

编辑: 我在mongoDB中为
User
Schema添加了一个示例,以便您更容易更改代码:

    // load the things we need
var mongoose = require('mongoose');
var bcrypt   = require('bcrypt-nodejs');


var userSchema = mongoose.Schema({

    local            : {
        email        : String,
        password     : String
    }
});

// generating a hash
userSchema.methods.generateHash = function(password) {
    return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};

// checking if password is valid
userSchema.methods.validPassword = 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);
//module.exports = userSchema;
module.exports.schema = userSchema;

您的
反序列化用户
正在引用未定义的变量(
电子邮件
)。您的策略处理程序没有调用
下一步()
当用户有效时。您正在使用未定义的变量
pass
。还不清楚您是如何将Express与Passport连接的。如果您对所有这些都不熟悉,也许您应该从示例开始。感谢您指出这些错误。我已使用新代码对其进行了更新。但它仍然不起作用。我已添加了我尝试将passport连接到express的e代码,Robert。不工作意味着您出现错误,它无法登录,登录失败…?该错误看起来您缺少路由。可能是/profile?它不工作。router.get('/'))应该可以工作,因为我单独创建了纵断面图。如果没有用于
/profile
的路由,则它将转到NotFound,您正在重定向到未定义的路由。是否尝试添加纵断面路由?出现了什么错误?是的。我添加了额外的路由。我获得了与以前相同的堆栈跟踪(我在问题中提出了这个问题)。您是否尝试在NotFound路由中添加console.log,打印请求的URL?您没有用于发布/登录的路由。您的登录URL(用于发布)看起来是
/signup/login
var configAuth=require('./auth'));//使用这个进行测试
给出了一个错误,它在我注释掉它之后运行。但是返回了与以前相同的错误。
    // load the things we need
var mongoose = require('mongoose');
var bcrypt   = require('bcrypt-nodejs');


var userSchema = mongoose.Schema({

    local            : {
        email        : String,
        password     : String
    }
});

// generating a hash
userSchema.methods.generateHash = function(password) {
    return bcrypt.hashSync(password, bcrypt.genSaltSync(8), null);
};

// checking if password is valid
userSchema.methods.validPassword = 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);
//module.exports = userSchema;
module.exports.schema = userSchema;