我正在開發一個node.js應用程序,並且已包含使用passportjs進行身份驗證。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: '/' })
});
我的登錄請求觸發,它在控制檯顯示消息,但就是這樣?
任何想法?
您好上述仍然不工作。由於某種原因,它不會觸及Localstrategy實現。 – tjhack
我使用Angularjs如果這有什麼區別?但是,發佈請求確實會在用戶對象中傳遞用戶名和密碼 – tjhack