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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
import { Router, Request, Response, Application } from 'express';
import { AkairoClient } from 'discord-akairo';
import fetch from 'node-fetch';
import session from 'express-session';
import OAuth2 from '../../structures/OAuth2';
import { callbackUrl, authorization, clientID, redirectUri, clientSecret } from '../../Config';
export default class OAuth2Router {
protected app: Application;
protected client: AkairoClient;
protected router: Router;
protected oauth: OAuth2;
public constructor(app: Application, client: AkairoClient, oauth: OAuth2) {
this.app = app;
this.client = client;
this.router = Router();
this.oauth = oauth;
this.app.use(session({
secret: authorization,
resave: false,
saveUninitialized: false,
cookie: {
secure: 'auto',
sameSite: false,
httpOnly: false,
maxAge: 6048e5
}
}));
this.app.use(this.router);
this.router.get('/oauth/login', (req: Request, res: Response) => {
return res.redirect(`https://discord.com/api/oauth2/authorize?client_id=${clientID}&redirect_uri=${encodeURIComponent(callbackUrl)}&response_type=code&scope=${encodeURIComponent('identify guilds')}`);
});
this.router.get('/oauth/logout', (req: Request, res: Response) => {
req.session.destroy(null);
return res.redirect(redirectUri);
});
this.router.get('/oauth/callback', (req: Request, res: Response) => {
fetch('https://discord.com/api/oauth2/token', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
//@ts-ignore
body: new URLSearchParams({
'client_id': clientID,
'client_secret': clientSecret,
'grant_type': 'authorization_code',
'code': req.query.code,
'redirect_uri': callbackUrl,
'scope': 'identify'
})
})
.then(response => response.json())
.then(response => {
req.session.token = response['access_token'];
res.redirect(redirectUri);
});
});
this.router.get('/oauth/details', async (req: Request, res: Response) => {
const details = await this.oauth.resolveInformation(req);
return res.status(200).send(details);
});
}
}
|