问题描述
我正在尝试将Passport用作Express-和Angular的身份验证中间件,如果有关系的话.我刚刚开始了解这些框架,并在应用程序中尝试了一个简单的测试登录.这些是我的root/app.js中应该执行登录方法的相关代码段:
I am trying to use Passport as an authentication middleware with Express - and Angular, if it matters. I have just started to know these frameworks and I experiment with a simple test login in the app. These are the relevant code snippets in my root/app.js that should do the login method:
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中的模板代码:
Here is my template code in 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')
我的问题是,如果我尝试发布,我会从浏览器得到以下响应:
And my problem is that if I try to post, I get this response from the browser:
响应无法发布/登录
ResponseCannot POST /login
推荐答案
您必须更新一些快递和护照要求,此要求才能生效.例如会话,failureflash消息,序列化和反序列化.这是为您更新的代码段.首先,您必须为此代码"failureFlash:true"行安装"connect-flash".但是它可以在没有闪光灯的情况下工作.
You have to update some of express and passport requirements for this to work. Such as session, failureflash messages, serialize and deserialize. Here is updated code snippet for you. first you have to install "connect-flash" for this line of codes "failureFlash : true". But it will work without 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 })
);
这是您的route/index.js文件的一些更新:
And here is a little update for your routes/index.js file:
exports.index = function(req, res){
var user = '';
if(req.session.passport.user !== undefined) user = req.user.username;
res.render('index', {title: user});
};
我希望这些可以帮助您了解使用护照登录的情况.
I hope these will help you to understand login with passport.
这篇关于无法发布在Express + Passport应用程序/身份验证测试中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!