/
index.ts
132 lines (117 loc) · 3.42 KB
/
index.ts
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import express from "express";
import session from "express-session";
import MongoStore from "connect-mongo";
import http from "http";
import {
authenticateUser,
createUserWithSessionId,
getUser,
getUserBySessionId,
} from "./users";
import bodyParser from "body-parser";
import cors from "cors";
import path from "path";
import { connectToDb, getDb } from "./db";
import passport from "passport";
import { Strategy as CustomStrategy } from "passport-custom";
import { Strategy as LocalStrategy } from "passport-local";
import { UserResponse } from "@ogfcommunity/variants-shared";
import { router as apiRouter } from "./api";
import * as socket_io from "./socket_io";
const LOCAL_ORIGIN = "http://localhost:3000";
passport.use(
new LocalStrategy(async function (username, password, callback) {
try {
const user = await authenticateUser(username, password);
if (!user) {
return callback("no user found");
}
return callback(null, user);
} catch (err) {
return callback(err);
}
}),
);
// Initialize MongoDB
connectToDb().catch((e) => {
console.log("Unable to connect to the database.");
console.log(e);
});
passport.use(
"guest",
new CustomStrategy(async function (req, callback) {
const token = req.session.id;
let user = await getUserBySessionId(token);
if (!user) {
user = await createUserWithSessionId(token);
}
return callback(null, user);
}),
);
passport.serializeUser<string>(function (user: UserResponse, callback) {
callback(null, user.id);
});
passport.deserializeUser<string>(function (id, callback) {
getUser(id)
.then((user) => {
if (user) {
callback(null, user);
} else {
callback(new Error(`No user with ID: ${id}`));
}
})
.catch((err) => {
callback(err);
});
});
// initialize Express
const app = express();
app.use(
bodyParser.urlencoded({
extended: true,
}),
);
app.use(bodyParser.json()); // TODO: app.use(express.json()) instead? Difference?
app.use(cors({ origin: LOCAL_ORIGIN, credentials: true })); // TODO: Is this still necessary with dev proxy?
app.use(
session({
// TODO: Cookie banner or permission necessary?
secret: process.env.SESSION_SECRET || "Corybas aconitiflorus",
resave: false,
saveUninitialized: false,
cookie: {
sameSite: "strict",
secure: "auto", // TODO: See https://www.npmjs.com/package/express-session
},
store: MongoStore.create({ client: getDb() }),
}),
);
app.use(passport.initialize());
app.use(passport.session());
// initialize socket.io
const server = http.createServer(app);
app.use("/api", apiRouter);
socket_io.init(server, LOCAL_ORIGIN);
const io = socket_io.io();
io.on("connection", (socket) => {
console.log("a user connected");
socket.on("ping", function (data) {
io.emit("pong", data);
console.log("ping");
});
});
// If production, serve the React repo!
const isProd = process.env.NODE_ENV === "production";
if (isProd) {
// Compute the build path and index.html path
const build_path = path.join(__dirname, "../../../packages/vue-client/dist");
const indexHtml = path.join(build_path, "index.html");
// Setup build path as a static assets path
app.use(express.static(build_path));
// Serve index.html on unmatched routes
app.get("*", (_req, res) => res.sendFile(indexHtml));
}
const PORT = process.env.PORT || 3001;
server.listen(PORT, () => {
console.log(`listening on *:${PORT}`);
});