无法在Express + Passport应用程序/身份验证测试中发布

时间:2013-06-03 10:08:38

标签: javascript node.js authentication express passport.js

我正在尝试使用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')

我的问题是,如果我尝试发帖,我会从浏览器获得此响应:

响应 无法发布/登录

1 个答案:

答案 0 :(得分: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');

然后更新快速环境:

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});
};

我希望这些能帮助您了解使用护照的登录信息。