authentication.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /* global WIKI */
  2. // ------------------------------------
  3. // Discord Account
  4. // ------------------------------------
  5. const DiscordStrategy = require('passport-discord').Strategy
  6. const _ = require('lodash')
  7. module.exports = {
  8. init (passport, conf) {
  9. passport.use(conf.key,
  10. new DiscordStrategy({
  11. clientID: conf.clientId,
  12. clientSecret: conf.clientSecret,
  13. authorizationURL: 'https://discord.com/api/oauth2/authorize?prompt=none',
  14. callbackURL: conf.callbackURL,
  15. scope: 'identify email guilds',
  16. passReqToCallback: true
  17. }, async (req, accessToken, refreshToken, profile, cb) => {
  18. try {
  19. if (conf.guildId && !_.some(profile.guilds, { id: conf.guildId })) {
  20. throw new WIKI.Error.AuthLoginFailed()
  21. }
  22. const user = await WIKI.models.users.processProfile({
  23. providerKey: req.params.strategy,
  24. profile: {
  25. ...profile,
  26. displayName: profile.username,
  27. picture: `https://cdn.discordapp.com/avatars/${profile.id}/${profile.avatar}.png`
  28. }
  29. })
  30. cb(null, user)
  31. } catch (err) {
  32. cb(err, null)
  33. }
  34. }
  35. ))
  36. }
  37. }