text: /**
   * React Starter Kit (https://www.reactstarterkit.com/)
   *
   * Copyright © 2014-2016 Kriasoft, LLC. All rights reserved.
   *
   * This source code is licensed under the MIT license found in the
   * LICENSE.txt file in the root directory of this source tree.
   */
  
  /**
   * Passport.js reference implementation.
   * The database schema used in this sample is available at
   * https://github.com/membership/membership.db/tree/master/postgres
   */
  
  import passport from 'passport';
  import { Strategy as FacebookStrategy } from 'passport-facebook';
  import { User, UserLogin, UserProfile } from '../data/models';
  import { auth as config } from '../config';
  
  /**
   * Sign in with Facebook.
   */
  passport.use(new FacebookStrategy({
    clientID: config.facebook.id,
    clientSecret: config.facebook.secret,
    callbackURL: config.facebook.callbackUrl,
    profileFields: ['name', 'email', 'displayName'],
    passReqToCallback: true,
  }, (req, accessToken, refreshToken, profile, done) => {
    /* eslint-disable no-underscore-dangle */
    const loginName = 'facebook';
    // const claimType = 'urn:facebook:access_token';
    const fooBar = async () => {
      if (req.user) {
        const userLogin = await UserLogin.findOne({
          attributes: ['name', 'key'],
          where: { name: loginName, key: profile.id },
        });
        if (userLogin) {
          // There is already a Facebook account that belongs to you.
          // Sign in with that account or delete it, then link it with your current account.
          done();
        } else {
          const user = await User.create({
            id: req.user.id,
            name: profile.displayName,
            email: profile._json.email,
            emailConfirmed: true,
            logins: [
              { name: loginName, key: profile.id },
            ],
            profile: {
              gender: profile._json.gender,
              picture: `https://graph.facebook.com/${profile.id}/picture?type=large`,
            },
          }, {
            include: [
              { model: UserLogin, as: 'logins' },
              { model: UserProfile, as: 'profile' },
            ],
          });
          done(null, {
            id: user.id,
            email: user.email,
          });
        }
      } else {
        const users = await User.findAll({
          attributes: ['id', 'email'],
          where: { '$logins.name$': loginName, '$logins.key$': profile.id },
          include: [
            {
              attributes: ['name', 'key'],
              model: UserLogin,
              as: 'logins',
              required: true,
            },
          ],
        });
        if (users.length) {
          done(null, {
            id: users[0].id,
            email: users[0].email,
          });
        } else {
          let user = await User.findOne({ where: { email: profile._json.email } });
          if (user) {
            // There is already an account using this email address. Sign in to
            // that account and link it with Facebook manually from Account Settings.
            done(null);
          } else {
            user = await User.create({
              name: profile.displayName,
              email: profile._json.email,
              emailConfirmed: true,
              logins: [
                { name: loginName, key: profile.id },
              ],
              profile: {
                gender: profile._json.gender,
                picture: `https://graph.facebook.com/${profile.id}/picture?type=large`,
              },
            }, {
              include: [
                { model: UserLogin, as: 'logins' },
                { model: UserProfile, as: 'profile' },
              ],
            });
            done(null, {
              id: user.id,
              email: user.email,
            });
          }
        }
      }
    };
  
    fooBar().catch(done);
  }));
  
  export default passport;
  