Nodejs Passport本地策略总是失败

时间:2015-12-23 12:42:41

标签: node.js authentication express passport.js

我是节点的全部用户身份验证新手,我正在尝试学习如何使用Passport的LocalStrategy将用户添加到Mongo数据库。

我正在尝试按照特定教程进行操作,并且由于某种原因,事情不会有计划。每当我提交注册表时,策略总是失败(被重定向到失败页面)。我有一种感觉,这与他请求不被传递的主体有关(因为我放置在宣布策略的地方没有运行)。然而,似乎当前的基础设施使得重构变得困难。可以重构此代码,以便在将请求交给护照之前手动解析请求(例如request.body。* name *)吗?

除非问题完全不同,否则我不知道......

index.js:

// Use middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(expressSession({ secret: 'whatkindofgamedoyouthinkthisishey',
                    cookie: {maxAge:null},
                    resave: false,
                    saveUninitialized: false}));
require("./config/passport")(passport);
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());

// Obtain application modules
var userModels = require("./schemas/user")(mongoose);
var loginPage = require("./routes/login")(passport, userModels);

// Initialize Routes
app.use("/", loginPage);

passport.js(应该有更好的名字):

var LocalStrategy = require("passport-local").Strategy,
    User = require("../schemas/user");

module.exports = function (passport) {
     passport.serializeUser(function (user, done) {
     done(null, user.id);
});
passport.deserializeUser(function (id, done) {
    User.findById(id, function (err, user) {
        done(err, user);
    });
});

passport.use("local-signup", new LocalStrategy({
        email: "email",
        password: "password",
        passReqToCallback: true
    },
    function (request, email, password, done) {
        console.log("message sent to sign up"); // log not running
        process.nextTick(function () {
            User.findOne({email: email}, function (err, user) {
                if (err) {
                    return done(err);
                }
                if (user) {
                    return done(null, false, request.flash("signupMessage", "That email is already taken"));
                } else {
                    var newUser = new User();
                    newUser.email = email;
                    newUser.password = password;

                    newUser.save(function (err) {
                        if (err) {
                            throw err;
                        } else {
                            return done(null, newUser);
                        }
                    });
                }
            });
        });
    }
));
};

login.js(正在导出的路由器)

router.post("/register", passport.authenticate("local-signup", {
    successRedirect: "/loggedIn",
    failureRedirect: "/connectFailed",
    failureFlash: false
}));

Html表格:

<form class="form-signin" action="/register" method="POST">
    <div class="logoContainer">
        <img src="images/LogoWithoutText.png" class="image image-responsive" id="loginImage">
    </div>
    <h2 class="form-signin-heading">Please sign in</h2>
    <label for="inputEmail" class="sr-only">Email address</label>
    <input type="email" id="inputEmail" class="form-control" name="email" placeholder="Email address" required autofocus>
    <label for="inputPassword" class="sr-only">Password</label>
    <input type="password" id="inputPassword" class="form-control" name="password" placeholder="Password" required>

    <div class="checkbox">
        <label>
            <input type="checkbox" value="remember-me"> Remember me
        </label>
    </div>
    <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>

1 个答案:

答案 0 :(得分:0)

从我在passportjs.org/docs中可以看到,看起来像passport.use有以下签名:

var passport = require('passport'), 
LocalStrategy = require('passport-local').Strategy;
passport.use(new LocalStrategy({
   email: "email",
   password: "password",
   passReqToCallback: true
},
function(req, email, password, done) {
 User.findOne({ username: username }, function(err, user) {
   if (err) { return done(err); }
   if (!user) {
    return done(null, false, { message: 'Incorrect username.' });
   }
   if (!user.validPassword(password)) {
    return done(null, false, { message: 'Incorrect password.' });
   }
   return done(null, user);
  });
 }
));

所以,尝试改变passport.use的编写方式并试试运气。