Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

brikis98 commented Aug 9, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.