简体   繁体   English

Passport.js LocalStrategy逻辑

[英]Passport.js LocalStrategy logic

I want to understand how does LocalStrategy work. 我想了解LocalStrategy如何工作。

Here is a part of my server file: 这是我的服务器文件的一部分:

var passport = require('passport');
var express = require('express');
/* other initializations */
var app = express();

passport.use = new LocalStrategy(
        function(email, password, done) {
            module.exports.findByUsername(email, function(err, user){
                if (err) throw err;
                if(!user) {
                    done(null, false, { message: 'Incorrect username.' });
                }
                else if(user.password != password) {
                    done(null, false, { message: 'Incorrect password.' });
                }
                else {
                    return done(null, user);
                } 
            });
        }
    )

app.post("/login" 
    , passport.authenticate('local',{
        successRedirect : "/",
        failureRedirect : "/login",
    }) ,
    function(){
        console.log("post /login");
    }
);

Now, from a client browser, I'm sending a http post request to http://localhost:8000/login . 现在,从客户端浏览器,我将向http://localhost:8000/login发送一个http发布请求。 If authentication is success then user will be redirected to the root page "/" and if failure, user will be redirected to login page again. 如果身份验证成功,则用户将被重定向到根页面"/" ,如果失败,则用户将被再次重定向到登录页面。

The question is, when we are defining a new LocalStrategy, I define a function(email,password, done){...} . 问题是,当我们定义新的LocalStrategy时,我定义了一个function(email,password, done){...} However, when I'm calling this function at app.post("/login", ...){...} how do I pass the email and password parameters? 但是,当我在app.post("/login", ...){...}调用此函数时app.post("/login", ...){...}如何传递电子邮件和密码参数?

passport assumes by default that you POST a form with input name='username' input name='password' . 护照默认情况下假设您以input name='username' input name='password'的形式过input name='username' override it as described in passport docs : 护照文档中所述覆盖它:

passport.use(new LocalStrategy({
    usernameField: 'email',
    passwordField: 'password'
  },
  function(email, password, done) {
    // ...
  }
));

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

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