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