-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDiscordStrategy.js
More file actions
50 lines (47 loc) · 1.24 KB
/
DiscordStrategy.js
File metadata and controls
50 lines (47 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const passport = require('passport');
const config = require('./config.json');
const DiscordUser = require('./models/user');
const functions = require('./functions/functions');
const DiscordStrategy = require('passport-discord').Strategy;
passport.serializeUser((user, done) => {
done(null, user.id);
});
passport.deserializeUser(async (id, done) => {
const user = await DiscordUser.findById(id);
if (user) {
done(null, user);
}
});
passport.use(
new DiscordStrategy(
{
clientID: config.clientID,
clientSecret: config.clientSecret,
callbackURL: config.callbackURL,
scope: ['identify'],
},
async (accessToken, refreshToken, profile, done) => {
try {
const user = await DiscordUser.findOne({ _id: profile.id });
if (user) {
await user.updateOne({
username: `${profile.username}#${profile.discriminator}`,
});
done(null, user);
} else {
const newUser = await DiscordUser.create({
_id: profile.id,
created: Date.now(),
token: functions.generateToken(),
username: `${profile.username}#${profile.discriminator}`,
});
const savedUser = await newUser.save();
done(null, savedUser);
}
} catch (err) {
console.log(err);
done(err, null);
}
},
),
);