2023-07-11 23:17:58 +02:00
|
|
|
const { Strategy: DiscordStrategy } = require('passport-discord');
|
|
|
|
const User = require('../models/User');
|
|
|
|
const config = require('../../config/loader');
|
|
|
|
const domains = config.domains;
|
|
|
|
|
2023-08-18 16:11:00 +02:00
|
|
|
const discordLogin = async (accessToken, refreshToken, profile, cb) => {
|
|
|
|
try {
|
|
|
|
const email = profile.email;
|
|
|
|
const discordId = profile.id;
|
|
|
|
const oldUser = await User.findOne({
|
|
|
|
email,
|
|
|
|
});
|
|
|
|
const ALLOW_SOCIAL_REGISTRATION =
|
|
|
|
process.env.ALLOW_SOCIAL_REGISTRATION?.toLowerCase() === 'true';
|
|
|
|
let avatarURL;
|
|
|
|
if (profile.avatar) {
|
|
|
|
const format = profile.avatar.startsWith('a_') ? 'gif' : 'png';
|
|
|
|
avatarURL = `https://cdn.discordapp.com/avatars/${profile.id}/${profile.avatar}.${format}`;
|
|
|
|
} else {
|
|
|
|
const defaultAvatarNum = Number(profile.discriminator) % 5;
|
|
|
|
avatarURL = `https://cdn.discordapp.com/embed/avatars/${defaultAvatarNum}.png`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldUser) {
|
|
|
|
oldUser.avatar = avatarURL;
|
|
|
|
await oldUser.save();
|
|
|
|
return cb(null, oldUser);
|
|
|
|
} else if (ALLOW_SOCIAL_REGISTRATION) {
|
|
|
|
const newUser = await new User({
|
|
|
|
provider: 'discord',
|
|
|
|
discordId,
|
|
|
|
username: profile.username,
|
|
|
|
email,
|
|
|
|
name: profile.global_name,
|
|
|
|
avatar: avatarURL,
|
|
|
|
}).save();
|
|
|
|
|
|
|
|
return cb(null, newUser);
|
|
|
|
}
|
|
|
|
|
|
|
|
return cb(null, false, {
|
|
|
|
message: 'User not found.',
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
return cb(err);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = () =>
|
2023-07-22 07:29:17 -07:00
|
|
|
new DiscordStrategy(
|
|
|
|
{
|
|
|
|
clientID: process.env.DISCORD_CLIENT_ID,
|
|
|
|
clientSecret: process.env.DISCORD_CLIENT_SECRET,
|
|
|
|
callbackURL: `${domains.server}${process.env.DISCORD_CALLBACK_URL}`,
|
2023-08-18 16:11:00 +02:00
|
|
|
scope: ['identify', 'email'],
|
|
|
|
authorizationURL: 'https://discord.com/api/oauth2/authorize?prompt=none',
|
2023-07-22 07:29:17 -07:00
|
|
|
},
|
2023-08-18 16:11:00 +02:00
|
|
|
discordLogin,
|
2023-07-22 07:29:17 -07:00
|
|
|
);
|