我正在开发一个node.js应用程序,并已使用passportjs进行身份验证。
但出于某种原因,由于某种原因,登录不会触发本地策略。我跟着这些例子,但仍然没有喜悦。我的代码在
之下var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');
var mysql = require('mysql') ;
var app = express();
var connection = null;
var encrypt = require('./node_custom_libraries/encrypt');
var bcrypt = require('bcrypt');
var passport = require('passport'),
LocalStrategy = require('passport-local').Strategy;
passport.serializeUser(function(user, done) {
done(null, user.id);
});
passport.deserializeUser(function(id, done) {
connection.query('SELECT * FROM Users WHERE id = ?', [id], function(err, user)
{
done(err,user);
});
});
passport.use(new LocalStrategy(
function(username, password, done) {
console.log('local strategy called');
connection.query('SELECT * FROM Users WHERE username = ?', [username], function(err, results)
{
console.log(results);
if (results.length == 0)
{
return done(null, false, false);
}
else
{
var comparePassword = encrypt.comparePassword(password, results[0].password, function(err,result)
{
if (err) { return done(err); }
console.log(result);
return done(null, result);
}
);
}
});
}
));
// all environments
app.configure(function(){
app.set('port', process.env.PORT || 8500);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.static(path.join(__dirname, 'app')));
// app setup for authentication
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.session({ secret: 'ecoSecret' }));
// Initialize Passport! Also use passport.session() middleware, to support
// persistent login sessions (recommended).
//app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
});
// development only
if ('development' == app.get('env')) {
//Connect to MYSQL database local database - DEV
app.use(express.errorHandler());
connection = mysql.createConnection({
host : '127.0.0.1',
user : 'xxx',
password : 'xxx',
database : 'xxx',
port : 'xxx'
});
connection.connect(function(err) {
// connected! (unless `err` is set)
if (!err)
{
console.log('database connected');
}
else
{
console.log(err.code); // 'ECONNREFUSED'
console.log(err.fatal); // true
}
});
}
//Add REST calls here
app.post('/login', function(req, res) {
console.log('req user' + req.body);
passport.authenticate('local', { successRedirect: '/admin/users',
failureRedirect: '/' })
});
我的登录请求会在控制台中显示消息,但就是这样吗?
有什么想法吗?
答案 0 :(得分:1)
我得到了这个工作。
基本上通过使用angularjs我发布了一个用户类型,即
user {username: 'xxx', password: 'xxx'}
但我特意将对象设置为发布以下结构:
{username: xxx, password: xxx}
答案 1 :(得分:0)
passport.authenticate
返回一个必须传递给Express的函数。这可行:
app.post('/login', function(req, res, next) {
console.log('req user' + req.body);
passport.authenticate('local', { successRedirect: '/admin/users',
failureRedirect: '/' })(req, res, next);
});
Express允许您为单个路径传递多个处理程序,因此您也可以执行以下操作:
app.post('/login',
passport.authenticate('local', { successRedirect: '/admin/users', failureRedirect: '/' }),
function (req, res) {
console.log('req user', req.body);
console.log('after auth', req.user);
}
);