Created
April 18, 2012 09:27
-
-
Save Enome/2412332 to your computer and use it in GitHub Desktop.
Express: basic route that verifies BrowserId
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This route can be used to complete the tutorial at: | |
// https://developer.mozilla.org/en/BrowserID/Quick_Setup | |
app.post('/api/login', function( req, res, next ) { | |
var options = { | |
host: 'browserid.org', | |
path: '/verify', | |
method: 'POST' | |
}; | |
var request = https.request(options, function (req) { | |
var body = ""; | |
req.on('data', function (chunk) { | |
body += chunk; | |
}); | |
req.on('end', function () { | |
try { | |
var response = JSON.parse(body); | |
if ( response.status === 'okay' ){ | |
res.send( { status: 'okay', email: response.email } ); | |
// Create your session and don't forget to setup your session middleware | |
// EXAMPLE: | |
// req.session.user = response.email; | |
} | |
else { | |
res.send( { status: 'error', reason: response.reason }) | |
}; | |
} | |
catch (e) { | |
res.send( { status: 'error', reason: 'Server error' }); | |
}; | |
}); | |
}); | |
request.setHeader("Content-Type", "application/x-www-form-urlencoded"); | |
var data = qs.stringify({ | |
assertion: req.body.assertion, | |
audience: '0.0.0.0:3000' | |
}); | |
request.setHeader("Content-Length", data.length); | |
request.write(data); | |
request.end(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment