-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord.js
41 lines (36 loc) · 1.12 KB
/
discord.js
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
const express = require('express');
const router = express.Router();
const DiscordOauth2 = require("discord-oauth2");
const profileSchema = require('../schemas/profile-schema');
const oauth = new DiscordOauth2();
require("dotenv").config();
const jwt = require('jsonwebtoken')
router.get('/discord', async (req, res) => {
let code = req.query.code;
if( code == undefined) {
res.send('Auth code is undefined')
}
else {
let data = await oauth.tokenRequest({
clientId: process.env.clientId,
clientSecret: process.env.clientSecret,
code: code,
scope: "identify guilds",
grantType: "authorization_code",
redirectUri: `${process.env.websiteURL}/discord`,
})
res.cookies.set("key", jwt.sign(data.access_token, process.env.jwtSecret))
let user = await oauth.getUser(data.access_token)
profileSchema.countDocuments({userId: user.id}, async (err, count) => {
if (count == 0) {
await new profileSchema({
userId: user.id,
}).save()
}
})
setTimeout(() => {
res.redirect('/settings')
}, 1000)
}
})
module.exports = router;