简体   繁体   English

Passport.js 执行

[英]Passport.js Execution

I have been looking at some passport.js tutorials online but haven't grasped a clear understanding of what is happening.我一直在网上查看一些passport.js教程,但还没有清楚地了解正在发生的事情。 Can someone help me clear my doubts below?有人可以帮我清除下面的疑问吗? Please read the paragraph at the bottom first.请先阅读底部的段落。

So assuming I set up everything correctly, this is the login strategy:所以假设我正确设置了一切,这是登录策略:

passport.use('login', new LocalStrategy({
    passReqToCallback : true
  },
  function(req, username, password, done) { 
    // check in mongo if a user with username exists or not
    User.findOne({ 'username' :  username }, 
      function(err, user) {
        // In case of any error, return using the done method
        if (err)
          return done(err);
        // Username does not exist, log error & redirect back
        if (!user){
          console.log('User Not Found with username '+username);
          return done(null, false, 
                req.flash('message', 'User Not found.'));                 
        }
        // User exists but wrong password, log the error 
        if (!isValidPassword(user, password)){
          console.log('Invalid Password');
          return done(null, false, 
              req.flash('message', 'Invalid Password'));
        }
        // User and password both match, return user from 
        // done method which will be treated like success
        return done(null, user);
      }
    );
}));

Now in my app.js (server) I have this as one of my routes:现在在我的 app.js(服务器)中,我将它作为我的路线之一:

/* Handle Login POST */
  router.post('/login', passport.authenticate('login', {
    successRedirect: '/home',
    failureRedirect: '/',
    failureFlash : true 
  }));

Now in my AJS file:现在在我的 AJS 文件中:

app.controller('loginController', function($scope) {
    var user = $resource('/login');

    $scope.createUser = function() {
        var User = new user();
        User.username = $scope.usernameVar;
        User.password = $scope.passwordVar;
        User.save();
    }
});

Please read this first (Instead of going through the code first):请先阅读此内容(而不是先阅读代码):

So when the user clicks on the login button on the login page the createUser function above is run (in my AJS file).因此,当用户单击登录页面上的登录按钮时,会运行上面的createUser函数(在我的 AJS 文件中)。 Then I create a resource object for the endpoint '/login' and when I call save on that it will run the route for that '/login' endpoint on my server (app.js).然后我为端点'/login'创建一个资源对象,当我调用 save 时,它​​将在我的服务器(app.js)上运行该'/login'端点的路由。 Then in my server it will passport.authenticate('login', ... which will run the passport middleware.然后在我的服务器中,它将passport.authenticate('login', ...它将运行passport 中间件。

Now my question is:现在我的问题是:

In the passport.use('login'... strategy where the do values for the variables req, username, and password come from in the callback to that strategy. Do I have to explicitly pass the username and password the user enters in the textfield on my front end. Like I have a two way data binding for those two textfields in AJS view. If so how do I pass those username and password values?passport.use('login'...策略中,变量req, username, and password的do 值来自该策略的回调中。我是否必须明确传递用户在我前端的文本字段。就像我在 AJS 视图中对这两个文本字段进行双向数据绑定一样。如果是这样,我该如何传递这些用户名和密码值?

Do these two lines in my AJS controller User.username = $scope.usernameVar;在我的 AJS 控制器中执行这两行User.username = $scope.usernameVar; and User.password = $scope.passwordVar;User.password = $scope.passwordVar; attach the usernameVar and passwordVar values to the req object on my server for the route '/login' ?将 usernameVar 和 passwordVar 值附加到我服务器req'/login'req对象?

If you have a form with action to post to your path /login , and have input names labeled after your username and password , the submit button will pass the values along to your passport code.如果您有一个带有操作的表单可以发布到您的路径/login ,并且在您的usernamepassword之后标记了输入名称,则提交按钮会将值传递给您的护照代码。

Check out Form in the docs .查看文档中的表单

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

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