Passport.js integration for FlatIron web framework.
This package allows Flatiron.js applications to easily use the Passport.js
authentication framework.
There are only two things that are different between using this API and using the regular Passport API.
1.) Instead of calling...
var express = require('express');
var passport = require('passport');
var app = express();
app.use(passport.initialize());
app.use(passport.session());
You simply need to call...
var flatiron = require('flatiron');
var fipassport = require('flatiron-passport');
var app = flatiron.app;
app.use(fipassport);
2.) Now anywhere you would use the variable passport
, you replace that with fipassport
in your app, like so...
passport.use(new LocalStrategy(function(username, password, done) {
...
...
});
passport.serializeUser(function(user, done) {
...
...
});
passport.deserializeUser(function(id, done) {
...
...
});
passport.authenticate(.....)
You simply call this instead...
fipassport.use(new LocalStrategy(function(username, password, done) {
...
...
});
fipassport.serializeUser(function(user, done) {
...
...
});
fipassport.deserializeUser(function(id, done) {
...
...
});
fipassport.authenticate(.....)
Please refer to the included example to get a better idea....
Install
npm install flatiron-passport
Example: From the example folder...
var fs = require('fs')
var flatiron = require('flatiron');
var LocalStrategy = require('passport-local').Strategy
var fipassport = require('flatiron-passport');
var app = flatiron.app;
var global_user = '';
var global_pass = '';
fipassport.use(new LocalStrategy(function(username, password, done) {
global_user = username;
global_pass = password;
done(null, {
id: 1234,
username: username,
password: password
});
}));
fipassport.serializeUser(function(user, done) {
done(null, user.id);
});
fipassport.deserializeUser(function(id, done) {
done(null, {
id:id,
username:global_user,
password:global_pass
});
});
app.use(flatiron.plugins.http);
app.use(fipassport);
app.router.get('/', function() {
if (this.req.isAuthenticated()) {
this.res.end('Hello ' + this.req.user.username);
}
else {
fs.readFile('index.html', (function(self) {
return function(err, data) {
if(err) {
self.res.writeHead(404);
self.res.end();
return;
}
self.res.writeHead(200, {'Content-Type': 'text/html'});
self.res.end(data);
};
})(this));
}
});
app.router.post('/login', fipassport.authenticate('local', {
successRedirect: '/',
failureRedirect: '/login'
}));
app.start(3000, function(){
console.log('HTTP Server started on port 3000');
});