All files / node-user-accounts-boilerplate-nahid/auth FacebookAuth.js

53.33% Statements 8/15
28.57% Branches 2/7
25% Functions 1/4
53.33% Lines 8/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70      1x 1x                               2x 2x         2x         2x         2x                                                               1x  
"use strict";
 
 
const Auth = require('./Auth');
const Strategy = require('passport-facebook')
  .Strategy;
 
/**
 * OAuth login using facebook login provider
 *
 * Requires ```passport-facebook``` package.
 */
class FacebookAuth extends Auth
{
 
  /**
   * @param {object} options see Auth class + additional options for email configuration.
   */
  constructor(options = {})
  {
    super('facebook', options);
    this.description.redirect = true;
 
    /**
     * OAuth 2 Client ID
     */
    this.facebookClientID = options.facebookClientID;
 
    /**
     * OAuth 2 Client Secret
     */
    this.facebookClientSecret = options.facebookClientSecret;
 
    /**
     * Should it construct urls based on proxy headers.
     */
    this.proxy = options.proxy || false;
  }
 
  /**
   * @override
   */
  install(app, prefix, passport)
  {
    passport.use(new Strategy({
        clientID: this.facebookClientID,
        clientSecret: this.facebookClientSecret,
        callbackURL: `${prefix}/callback.json`,
        scope: ['email', 'public_profile'],
        state: true,
        passReqToCallback: true,
        proxy: this.proxy,
      }, (req, accessToken, refreshToken, profile, done) =>
      this.handleUserLoginByProfile(null, profile, done, req)));
 
    app.all(`${prefix}/login.json`, passport.authenticate('facebook', {}));
 
    app.all(`${prefix}/callback.json`, passport.authenticate('facebook', this.authenticateOptions), this.loggedIn(true));
  }
 
  createUserFromProfile(profile)
  {
    profile._json = profile._json || {};
    profile._json.publicProfileUrl = profile._json.publicProfileUrl || `https://www.facebook.com/${profile.id}`;
    return super.createUserFromProfile(profile);
  }
}
 
module.exports = FacebookAuth;