I am relatively new to nodejs and particaullary new to expressjs framework.
I need to implement and simple local authentication for my web app. I decided to use passportjs with LocalStrategy to achive this.
I read trough the documentation and looked through some examples, but I can't seem to get it to work.
Here is how I setup up passportjs. I have moved it in a separate module in order not to clutter the app.js.
var passport = require('passport');
var session = require('express-session');
var flash = require('connect-flash');
var express = require('express');
var router = express.Router();
var LocalStrategy = require('passport-local');
module.exports = function() {
function setupPassport(app) {
setupPassportSettings(app, passport);
app.use(session({ secret: 'expensestracker' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
setupRoutes(app, passport);
}
function setupPassportSettings(app, passport){
passport.use(new LocalStrategy({
usernameField: 'email',
passwordField: 'password'
},
function(username, password, done) {
// **this is never called...**
console.log('trying to understand if local startegy will be called...');
}
));
console.log('local strategy is setup...');
}
function setupRoutes(app, passport) {
router.get('/signin', function(req, res){
console.log('this will be sign in page...');
res.send('sign in page');
});
router.post('/signin', function(req, res){
console.log('this will be invoked when usesrs posts the login form...');
res.send('signed in...');
});
app.post('/signin', passport.authenticate('local', { successRedirect: '/',
failureRedirect: '/login' }));
console.log('passport routes registered');
}
return {
setupPassport : setupPassport
};
}();
Here is my package.json file. Maybe it has something to do with versions of the packages.
{
"name": "nodetraining",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "~1.0.0",
"connect-flash": "^0.1.1",
"cookie-parser": "~1.0.1",
"debug": "~0.7.4",
"express": "~4.2.0",
"express-session": "^1.8.2",
"jade": "~1.3.0",
"mongodb": "*",
"monk": "*",
"morgan": "~1.0.0",
"passport": "^0.2.1",
"passport-local": "^1.0.0",
"static-favicon": "~1.0.0"
}
}
Here is the part of app.js where I call my module to setup the passport
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
passportSetup.setupPassport(app);
app.use(favicon());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
So it looks like the LocalStrategy call back function is never invoked and I am allowed to access any resource in my web app.