-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
96 lines (85 loc) · 2.41 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const express = require("express");
const app = express();
const passport = require("passport");
const session = require("express-session");
const DiscordStrategy = require("passport-discord").Strategy;
const config = require("./config.js");
const bodyParser = require("body-parser");
const path = require("path");
const fs = require("fs");
const ejs = require("ejs");
const port = 3000;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set("views", path.join(__dirname, "./www"));
app.use(express.static("web"));
app.use("/css", express.static(path.join(__dirname, "./css")));
app.set("view engine", "ejs");
app.use(
session({
secret: "YOUR_SECRET_KEY",
resave: false,
saveUninitialized: true,
})
); // Session
app.use(passport.initialize());
app.use(passport.session());
passport.serializeUser((user, done) => {
done(null, user);
});
passport.deserializeUser((obj, done) => {
done(null, obj);
});
passport.use(
new DiscordStrategy(
{
clientID: config.clientID,
clientSecret: config.clientSecret,
callbackURL: config.callbackURL,
scope: config.scopes,
},
(accessToken, refreshToken, profile, done) => {
process.nextTick(() => done(null, profile));
}
)
);
app.get(
"/auth/oauth2/discord",
passport.authenticate("discord", { scope: config.scopes }),
(req, res) => {}
);
app.get(
"/auth/oauth2/discord/callback",
passport.authenticate("discord", { failureRedirect: "/" }),
(req, res) => {
res.redirect("/");
}
);
app.get("/logout", (req, res) => {
if(req.user) {
req.logout(function(error) {
if (error) {
console.log(error);
res.sendStatus(500);
}
});
}
res.redirect("/");
});
app.get("/", (req, res) => {
if (req.user) {
let avatarURL = `https://cdn.discordapp.com/avatars/${req.user.id}/${req.user.avatar}.png`;
let bannerURL;
if (req.user.banner) {
bannerURL = `https://cdn.discordapp.com/banners/${req.user.id}/${req.user.banner}.png`;
} else {
bannerURL = "https://i.hizliresim.com/6aq61dk.jpg";
}
res.render("index", { user: req.user, avatarURL: avatarURL, bannerURL: bannerURL });
} else {
res.render("index", { user: req.user });
}
});
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});