authentication.js 992 Bytes
Newer Older
1
/* global WIKI */
2 3 4 5 6 7

// ------------------------------------
// Facebook Account
// ------------------------------------

const FacebookStrategy = require('passport-facebook').Strategy
Nick's avatar
Nick committed
8
const _ = require('lodash')
9

10 11
module.exports = {
  init (passport, conf) {
12
    passport.use(conf.key,
13 14 15 16
      new FacebookStrategy({
        clientID: conf.clientId,
        clientSecret: conf.clientSecret,
        callbackURL: conf.callbackURL,
Nick's avatar
Nick committed
17
        profileFields: ['id', 'displayName', 'email', 'photos'],
18 19 20
        authType: 'reauthenticate',
        passReqToCallback: true
      }, async (req, accessToken, refreshToken, profile, cb) => {
Nick's avatar
Nick committed
21 22
        try {
          const user = await WIKI.models.users.processProfile({
23
            providerKey: req.params.strategy,
Nick's avatar
Nick committed
24 25 26
            profile: {
              ...profile,
              picture: _.get(profile, 'photos[0].value', '')
27
            }
Nick's avatar
Nick committed
28 29 30 31 32
          })
          cb(null, user)
        } catch (err) {
          cb(err, null)
        }
33 34 35
      }
      ))
  }
36
}