Skip to content

Instantly share code, notes, and snippets.

@koostudios
Created May 5, 2012 08:32
Show Gist options
  • Save koostudios/2600946 to your computer and use it in GitHub Desktop.
Save koostudios/2600946 to your computer and use it in GitHub Desktop.
BrowserID: Logging in with BrowserID, Passport and NodeJS
$("#browserid").on('click', function(e) {
e.preventDefault()
navigator.id.get(function(assertion) {
if (assertion) {
$("input[name=assertion]").val(assertion);
$("form").submit();
} else {
location.reload();
}
});
});
form(method= 'post', action= '/login')
input(name= 'assertion', type= 'hidden')
input#browserid(type= 'submit', value= 'Sign in with BrowserID')
!!! 5
html(lang= 'en')
head
meta(charset= 'utf-8')
title= 'BrowserID with Passport and NodeJS'
body
!=body
script(src= 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js')
script(src= 'https://browserid.org/include.js')
script(src= '/client.js')
# Variables
exp = require 'express'
app = exp.createServer()
pass = require 'passport'
BrowserID = require('passport-browserid').Strategy
# Passport Serialize and Deserialize Functions
pass.serializeUser (user, done) ->
done null, user.email
pass.deserializeUser (email, done) ->
done null, {email: email}
# Passport-BrowserID Strategy
pass.use 'browserid', new BrowserID {audience: 'http://dev.koostudios.com:1337'}, (email, done) ->
# Interface with Database would go here
done null, {email: email}
# App Configuration
app.configure () ->
app.set 'view engine', 'jade'
app.set 'views', __dirname + '/views'
app.use exp.static __dirname + '/public'
app.use exp.cookieParser()
app.use exp.bodyParser()
app.use exp.methodOverride()
app.use exp.session {secret: 'somesecretstringhere'}
app.use pass.initialize()
app.use pass.session()
app.use app.router
# Run App
app.listen '1337'
console.log 'Server running at port ' + app.address().port
# Routes
app.get '/', (req, res) ->
if req.isAuthenticated()
res.send "Congratulations! You've signed in as " + req.user.email
else
res.render 'index'
app.post '/login', pass.authenticate 'browserid',
successRedirect: '/'
failureRedirect: '/'
failureFlash: true
{
"name": "browserid-tutorial",
"description": "BrowserID: Logging in with BrowserID, Passport and NodeJS",
"version": "0.1.0",
"author": "Alexander Yuen <[email protected]> (http://www.koostudios.com/)",
"main": "server.js",
"node": "0.6.x",
"dependencies": {
"coffee-script" : "1.2.x",
"express" : "2.5.x",
"jade" : "0.20.x",
"passport" : "0.1.x",
"passport-browserid": "0.1.x"
},
"license": "MIT"
}
require('coffee-script');
require('./main');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment