Skip to content

Instantly share code, notes, and snippets.

@brikis98
Created June 12, 2011 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brikis98/1021322 to your computer and use it in GitHub Desktop.
Save brikis98/1021322 to your computer and use it in GitHub Desktop.
LinkedIn Auth Middleware
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
@brikis98
Copy link
Author

brikis98 commented Aug 9, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment