-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (69 loc) · 2.45 KB
/
index.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
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
72
73
74
75
const client = require('../oauth');
const express = require('express');
const router = express.Router();
const verify = async (req, res, next) => {
if (req.cookies['user-key']) {
try {
const validity = client.checkValidity(req.cookies['user-key']);
if (validity.expired) {
const newKey = await client.refreshToken(req.cookies['user-key']);
res.cookie('user-key', newKey);
}
next();
} catch (err) {
res.cookie('user-key', 'deleted', { maxAge: -1 });
res.redirect('/');
}
} else res.redirect('/');
}
router.get('/', async (req, res) => {
if (req.cookies['user-key']) {
try {
const keyValidity = client.checkValidity(req.cookies['user-key']);
if (keyValidity.expired) {
const newKey = await client.refreshToken(req.cookies['user-key']);
res.cookie('user-key', newKey);
res.redirect('/home');
} else {
res.redirect('/home');
}
} catch (err) {
console.error(err);
const { link, state } = client.auth;
res.cookie('user-key', 'deleted', { maxAge: -1 });
res.cookie('user-state', state);
res.render('index', { title: 'Scratch For Discord', link });
}
} else {
const { link, state } = client.auth;
res.cookie('user-state', state);
res.render('index', { title: 'Scratch For Discord', link });
}
});
router.get('/profile', verify, async (req, res) => {
const user = await client.users.fetch(req.cookies['user-key']);
res.render('profile', { title: 'Profile- S4D', user });
});
router.get('/home', verify, async (req, res) => {
const user = await client.users.fetch(req.cookies['user-key']);
res.render('home', { title: 'Home - S4D', user });
});
router.get('/login', async (req, res) => {
if (req.query.state && req.query.code && req.cookies['user-state']) {
if (req.query.state === req.cookies['user-state']) {
const userKey = await client.getAccess(req.query.code).catch(console.error);
res.cookie('user-state', 'deleted', { maxAge: -1 });
res.cookie('user-key', userKey);
res.redirect('/');
} else {
res.send('States do not match. Nice try hackerman!');
}
} else {
res.send('Invalid login request.');
}
});
router.get('/logout', (req, res) => {
res.cookie('user-key', 'deleted', { maxAge: -1 });
res.redirect('/');
});
module.exports = router;