UNPKG

passport-meetup

Version:

Meetup authentication strategy for Passport.

99 lines (87 loc) 2.9 kB
/** * Module dependencies. */ var util = require('util') , OAuthStrategy = require('passport-oauth1') , InternalOAuthError = require('passport-oauth1').InternalOAuthError; /** * `Strategy` constructor. * * The Meetup authentication strategy authenticates requests by delegating to * Meetup using the OAuth protocol. * * Applications must supply a `verify` callback which accepts a `token`, * `tokenSecret` and service-specific `profile`, and then calls the `done` * callback supplying a `user`, which should be set to `false` if the * credentials are not valid. If an exception occured, `err` should be set. * * Options: * - `consumerKey` identifies client to Meetup * - `consumerSecret` secret used to establish ownership of the consumer key * - `callbackURL` URL to which Meetup will redirect the user after obtaining authorization * * Examples: * * passport.use(new MeetupStrategy({ * consumerKey: '123-456-789', * consumerSecret: 'shhh-its-a-secret' * callbackURL: 'https://www.example.net/auth/meetup/callback' * }, * function(token, tokenSecret, profile, done) { * User.findOrCreate(..., function (err, user) { * done(err, user); * }); * } * )); * * @param {Object} options * @param {Function} verify * @api public */ function Strategy(options, verify) { options = options || {}; options.requestTokenURL = options.requestTokenURL || 'https://api.meetup.com/oauth/request/'; options.accessTokenURL = options.accessTokenURL || 'https://api.meetup.com/oauth/access/'; options.userAuthorizationURL = options.userAuthorizationURL || 'http://www.meetup.com/authenticate/'; options.sessionKey = options.sessionKey || 'oauth:meetup'; OAuthStrategy.call(this, options, verify); this.name = 'meetup'; } /** * Inherit from `OAuthStrategy`. */ util.inherits(Strategy, OAuthStrategy); /** * Retrieve user profile from Meetup. * * This function constructs a normalized profile, with the following properties: * * - `id` * - `displayName` * * @param {String} token * @param {String} tokenSecret * @param {Object} params * @param {Function} done * @api protected */ Strategy.prototype.userProfile = function(token, tokenSecret, params, done) { this._oauth.get('https://api.meetup.com/2/members?member_id=self', token, tokenSecret, function (err, body, res) { if (err) { return done(new InternalOAuthError('failed to fetch user profile', err)); } try { var json = JSON.parse(body); var profile = { provider: 'meetup' }; profile.id = json.results[0].id profile.displayName = json.results[0].name; profile._raw = body; profile._json = json; done(null, profile); } catch(e) { done(e); } }); } /** * Expose `Strategy`. */ module.exports = Strategy;