1

我正在尝试使用 Passport 作为 Express 和 Angular 的身份验证中间件,如果重要的话。我刚刚开始了解这些框架,并在应用程序中尝试了一个简单的测试登录。这些是我的 root/app.js 中应该执行登录方法的相关代码片段:

var express = require('express'),
  routes = require('./routes'),
  api = require('./routes/api'),
  passport = require('passport'),
  LocalStrategy = require('passport-local').Strategy;

...

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.static(__dirname + '/public'));
  app.use(passport.initialize());
  app.use(app.router);
});

...

// Authentication with Passport.js

var testUser = {
  username: 'patrik',
  password: '123'
}

passport.use(new LocalStrategy(
    function(username, password, done) {
      if(username == testUser.username) {
        if(password == testUser.password) {
          return done(null, username);
        } else {
          return done(null, false, { message: 'Incorrect password.' });
        }
      } else {
        return done(null, false, { message: 'Incorrect username.' });
      }
    }
));

...

app.post('/login',
    passport.authenticate('local', { successRedirect: '/',
      failureRedirect: '/login',
      failureFlash: true })
);

这是我在 index.jade 中的模板代码:

form(method='post',action='/login')
      div
        label Username:
        input(type='text',name='username')
      div
        label Password:
        input(type='password',name='password')
      div
        input(type='submit',value='Log in')

我的问题是,如果我尝试发布,我会从浏览器中得到以下响应:

响应无法 POST /login

4

1 回答 1

0

您必须更新一些快递和护照要求才能正常工作。比如session、failureflash消息、序列化和反序列化。这是为您更新的代码片段。首先,您必须为这行代码“failureFlash:true”安装“connect-flash”。但它可以在没有闪光灯的情况下工作。

npm install connect-flash

var express = require('express'),
    routes = require('./routes'),
    api = require('./routes/api'),
    passport = require('passport'),
    LocalStrategy = require('passport-local').Strategy,
    flash = require('connect-flash');

然后更新 express 环境:

app.configure(function(){
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(express.static(__dirname + '/public'));
    app.use(express.cookieParser());
    // required for passport
    app.use(express.session({ secret: 'my secret'}));
    app.use(passport.initialize());
    app.use(passport.session());
    app.use(app.router);
});

// Authentication with Passport.js

var testUser = {
  username: 'patrik',
  password: '123'
}

passport.use(new LocalStrategy(
  function(username, password, done) {

    if (username === testUser.username && password === testUser.password) {
      return done(null, {username: username});
    } else {
      return done(null, {username: 'username or password incorrect!'});
    }

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

passport.serializeUser(function(user, done) {
  done(null, user.username);
});

passport.deserializeUser(function(username, done) {
  done(null, {username: username});
});

app.get('/', routes.index);

app.post('/login',
    passport.authenticate('local', { 
        successRedirect: '/',
        failureRedirect: '/login',
        failureFlash: true })
    );

这是您的 routes/index.js 文件的一些更新:

exports.index = function(req, res){
    var user = '';
    if(req.session.passport.user !== undefined) user = req.user.username;

    res.render('index', {title: user});
};

我希望这些能帮助您理解使用护照登录。

于 2014-01-06T03:58:17.693 回答