-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
67 lines (59 loc) · 1.98 KB
/
server.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
var express = require('express')
, session = require('express-session')
, passport = require('passport')
, Strategy = require('passport-discord').Strategy
, app = express()
, fs = require("fs");
passport.serializeUser(function (user, done) {
done(null, user);
});
passport.deserializeUser(function (obj, done) {
done(null, obj);
});
var scopes = ['bot', 'identify', 'email'];
var permissions = 268444722;
const config = require("./config.json");
passport.use(new Strategy({
clientID: config.CLIENT_ID,
clientSecret: config.CLIENT_SECRET,
callbackURL: 'http://localhost:5000/callback',
scope: scopes,
permissions: permissions
}, function (accessToken, refreshToken, profile, done) {
process.nextTick(function () {
console.log("AccessToken: " + accessToken);
console.log("RefreshToken: " + refreshToken);
config.accessToken = accessToken;
config.refreshToken = refreshToken;
return fs.writeFile('config.json', JSON.stringify(config), 'utf8', () => done(null, profile));
//return done(null, profile);
});
}));
app.use(session({
secret: 'qutopia',
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
app.get('/', passport.authenticate('discord', { scope: scopes, permissions: permissions }), function (req, res) { });
app.get('/callback',
passport.authenticate('discord', { failureRedirect: '/' }), function (req, res) { res.redirect('/info') } // auth success
);
app.get('/logout', function (req, res) {
req.logout();
res.redirect('/');
});
app.get('/info', checkAuth, function (req, res) {
//console.log(req.user)
res.json(req.user);
setTimeout(() => process.exit(), 5000);
});
function checkAuth(req, res, next) {
if (req.isAuthenticated()) return next();
res.send('not logged in :(');
}
app.listen(5000, function (err) {
if (err) return console.log(err)
console.log('Listening at http://localhost:5000/')
})