examples/signin/app.js
var express = require('express')
, passport = require('passport')
, util = require('util')
, BrowserIDStrategy = require('../..').Strategy;
// Passport session setup.
// To support persistent login sessions, Passport needs to be able to
// serialize users into and deserialize users out of the session. Typically,
// this will be as simple as storing the user ID when serializing, and finding
// the user by ID when deserializing. However, since this example does not
// have a database of user records, the BrowserID verified email address
// is serialized and deserialized.
passport.serializeUser(function(user, done) {
done(null, user.email);
});
passport.deserializeUser(function(email, done) {
done(null, { email: email });
});
// Use the BrowserIDStrategy within Passport.
// Strategies in passport require a `validate` function, which accept
// credentials (in this case, a BrowserID verified email address), and invoke
// a callback with a user object.
passport.use(new BrowserIDStrategy({
audience: 'http://127.0.0.1:3000'
},
function(email, done) {
// asynchronous verification, for effect...
process.nextTick(function () {
// To keep the example simple, the user's email address is returned to
// represent the logged-in user. In a typical application, you would want
// to associate the email address with a user record in your database, and
// return that user instead.
return done(null, { email: email })
});
}
));
var app = express.createServer();
// configure Express
app.configure(function() {
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.logger());
app.use(express.cookieParser());
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.session({ secret: 'keyboard cat' }));
// Initialize Passport! Also use passport.session() middleware, to support
// persistent login sessions (recommended).
app.use(passport.initialize());
app.use(passport.session());
app.use(app.router);
app.use(express.static(__dirname + '/../../public'));
});
app.get('/', function(req, res){
res.render('index', { user: req.user });
});
app.get('/account', ensureAuthenticated, function(req, res){
res.render('account', { user: req.user });
});
app.get('/login', function(req, res){
res.render('login', { user: req.user });
});
// POST /auth/browserid
// Use passport.authenticate() as route middleware to authenticate the
// request. BrowserID authentication will verify the assertion obtained from
// the browser via the JavaScript API.
app.post('/auth/browserid',
passport.authenticate('browserid', { failureRedirect: '/login' }),
function(req, res) {
res.redirect('/');
});
app.get('/logout', function(req, res){
req.logout();
res.redirect('/');
});
app.listen(3000);
// Simple route middleware to ensure user is authenticated.
// Use this route middleware on any resource that needs to be protected. If
// the request is authenticated (typically via a persistent login session),
// the request will proceed. Otherwise, the user will be redirected to the
// login page.
function ensureAuthenticated(req, res, next) {
if (req.isAuthenticated()) { return next(); }
res.redirect('/login')
}