2018-03-05 20:49:36 +00:00
|
|
|
/* global WIKI */
|
2017-12-31 19:40:28 +00:00
|
|
|
|
|
|
|
// ------------------------------------
|
|
|
|
// Discord Account
|
|
|
|
// ------------------------------------
|
|
|
|
|
|
|
|
const DiscordStrategy = require('passport-discord').Strategy
|
2020-03-07 22:59:10 +00:00
|
|
|
const _ = require('lodash')
|
2017-12-31 19:40:28 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
init (passport, conf) {
|
|
|
|
passport.use('discord',
|
|
|
|
new DiscordStrategy({
|
|
|
|
clientID: conf.clientId,
|
|
|
|
clientSecret: conf.clientSecret,
|
2019-08-04 19:56:46 +00:00
|
|
|
authorizationURL: 'https://discordapp.com/api/oauth2/authorize?prompt=none',
|
2017-12-31 19:40:28 +00:00
|
|
|
callbackURL: conf.callbackURL,
|
2020-03-07 22:59:10 +00:00
|
|
|
scope: 'identify email guilds'
|
2019-04-22 01:43:33 +00:00
|
|
|
}, async (accessToken, refreshToken, profile, cb) => {
|
|
|
|
try {
|
2020-03-07 22:59:10 +00:00
|
|
|
if (conf.guildId && !_.some(profile.guilds, { id: conf.guildId })) {
|
|
|
|
throw new WIKI.Error.AuthLoginFailed()
|
|
|
|
}
|
2019-04-22 01:43:33 +00:00
|
|
|
const user = await WIKI.models.users.processProfile({
|
|
|
|
profile: {
|
|
|
|
...profile,
|
|
|
|
displayName: profile.username,
|
|
|
|
picture: `https://cdn.discordapp.com/avatars/${profile.id}/${profile.avatar}.png`
|
|
|
|
},
|
|
|
|
providerKey: 'discord'
|
|
|
|
})
|
|
|
|
cb(null, user)
|
|
|
|
} catch (err) {
|
|
|
|
cb(err, null)
|
|
|
|
}
|
2017-12-31 19:40:28 +00:00
|
|
|
}
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|