Created
June 12, 2011 07:10
-
-
Save brikis98/1021322 to your computer and use it in GitHub Desktop.
LinkedIn Auth Middleware
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
LinkedInClient = require('./LinkedInClient').LinkedInClient | |
_ = require 'underscore' | |
url = require 'url' | |
exports.linkedinAuth = (apiKey, apiSecret, baseUrl, secureUrls) -> | |
(req, res, next) -> | |
req.session.tokens ?= {} | |
req.linkedInClient = new LinkedInClient {apiKey: apiKey, apiSecret: apiSecret}, req.session.tokens | |
handleError = (error) -> | |
console.log "Error logging in: #{JSON.stringify error}" | |
return res.send "There was an error logging into LinkedIn. Please try again." | |
if req.linkedInClient.hasAccessToken() || !(req.url && _.include secureUrls, url.parse(req.url).pathname) | |
next() | |
else if req.linkedInClient.hasRequestToken() && req.query['oauth_verifier'] | |
req.linkedInClient.getAccessToken req.query['oauth_verifier'], (error) -> | |
return handleError error if error | |
next() | |
else | |
req.linkedInClient.getRequestToken "#{baseUrl}#{req.url}", (error, redirectUrl) -> | |
return handleError error if error | |
res.redirect redirectUrl | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See CoffeeScript: JavaScript that’s easy on the eyes for more information.