使用PassportJS进行身份验证始终会失败

时间:2020-03-25 15:53:04

标签: passport.js

所以我真的很难获得PassportJS。我读过他们的文档几次,但我听不懂。如果我使用curls向/login发送随机发帖请求,为什么会失败?我认为我应该在任何可能的情况下都取得成功。

var express = require('express');
var passport = require('passport');
var Strategy = require('passport-local').Strategy;

// Configure the local strategy for use by Passport.
passport.use(new Strategy(
  function(username, password, done) {
    var user = { id: 1, username: 'jack', password: 'secret', displayName: 'Jack', emails: [ { value: 'jack@example.com' } ] }

    return done(null, user);
  }));

// Create a new Express application.
var app = express();

app.use(passport.initialize());


// Enable CORS
app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*"); // update to match the domain you will make the request from
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

// Routes
app.get('/',
  function(req, res) {
    res.send('hello :)');
  });

// -- Login
app.post('/login',
  passport.authenticate('local', { successRedirect: '/success',
                                   failureRedirect: '/failx', })
);

var port = process.env.PORT || 3000;
app.listen(port);

如您所见,在我们策略的验证功能中,我只设置了一些随机用户对象,并且将错误设置为null。那么为什么我应该重定向到/ fail?

0 个答案:

没有答案