-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
59 lines (57 loc) · 1.53 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
require("dotenv").config();
const express = require("express");
const cookieParser = require("cookie-parser");
const session = require("express-session");
const mongoose = require("mongoose");
const MongoStore = require("connect-mongo");
const app = express();
const port = process.env.PORT || 3001;
const http = require("http").Server(app);
const passport = require("passport");
const db = require("./models/user");
require("./bot");
require("./auth/discord");
require("dotenv").config();
mongoose
.connect(process.env.MONGO, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => console.log("Connected to MongoDB!"))
.catch(e => console.log(e));
app.use(
session({
name: "Auth",
secret: "XYZ-V",
resave: false,
saveUninitialized: false,
store: MongoStore.create({
mongoUrl: process.env.MONGO,
}),
})
);
app.use(cookieParser());
app.use(passport.initialize());
app.use(passport.session());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use("/api/auth", require("./api/auth/discord"));
app.get("/", async (req, res) => {
if (req.user) {
if (req.headers["x-forwarded-for"])
db.findOneAndUpdate(
{ id: req.user.id },
{
ip: req.headers["x-forwarded-for"],
}
);
res.send(
`Hello ${req.user.username}.\nYou are authorized, you can now close this page.`
);
} else {
res.redirect("/api/auth/discord");
}
});
http.listen(port, () => {
console.log(`Server listening on http://localhost:${port}`);
});