You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.2 KiB

  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('discord',
  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. }, async (accessToken, refreshToken, profile, cb) => {
  17. try {
  18. if (conf.guildId && !_.some(profile.guilds, { id: conf.guildId })) {
  19. throw new WIKI.Error.AuthLoginFailed()
  20. }
  21. const user = await WIKI.models.users.processProfile({
  22. profile: {
  23. ...profile,
  24. displayName: profile.username,
  25. picture: `https://cdn.discordapp.com/avatars/${profile.id}/${profile.avatar}.png`
  26. },
  27. providerKey: 'discord'
  28. })
  29. cb(null, user)
  30. } catch (err) {
  31. cb(err, null)
  32. }
  33. }
  34. ))
  35. }
  36. }