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

// ------------------------------------
// Discord Account
// ------------------------------------

const DiscordStrategy = require('passport-discord').Strategy
8
const _ = require('lodash')
9 10 11 12 13 14 15

module.exports = {
  init (passport, conf) {
    passport.use('discord',
      new DiscordStrategy({
        clientID: conf.clientId,
        clientSecret: conf.clientSecret,
16
        authorizationURL: 'https://discord.com/api/oauth2/authorize?prompt=none',
17
        callbackURL: conf.callbackURL,
18 19 20
        scope: 'identify email guilds',
        passReqToCallback: true
      }, async (req, accessToken, refreshToken, profile, cb) => {
21
        try {
22
          if (conf.guildId && !_.some(profile.guilds, { id: conf.guildId })) {
23 24
            throw new WIKI.Error.AuthLoginFailed()
          }
25
          const user = await WIKI.models.users.processProfile({
26
            providerKey: req.params.strategy,
27 28 29 30
            profile: {
              ...profile,
              displayName: profile.username,
              picture: `https://cdn.discordapp.com/avatars/${profile.id}/${profile.avatar}.png`
31
            }
32 33 34 35 36
          })
          cb(null, user)
        } catch (err) {
          cb(err, null)
        }
37 38 39 40
      }
      ))
  }
}