-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
77 lines (53 loc) · 1.99 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
import * as https from 'node:https';
import express from 'express';
import session from 'express-session';
import MongoStore from 'connect-mongo';
import fs from 'node:fs';
import path from 'node:path';
import { authRouter, getAuthUrl } from './api/auth/auth.js';
import { profileRouter } from './api/profile/profile.js';
import { itemsRouter } from './api/items/item.js';
import dbConnectConfig from './api/dbconnect.config.json' assert {type: "json"};
import sslConfig from './ssl.config.json' assert {type: "json"};
var app = express();
app.use(session({
resave: false,
saveUninitialized: false,
secret: 'dd9s02a2f9dsa',
cookie: { secure: true, sameSite: 'lax' },
store: MongoStore.create({mongoUrl: `mongodb://${dbConnectConfig.userName}:${dbConnectConfig.password}@${dbConnectConfig.server}:${dbConnectConfig.port}/d2lm?authSource=${dbConnectConfig.authSource}`})
}));
const __dirname = path.resolve();
const sslOptions = {
key: fs.readFileSync(sslConfig.keyPath),
cert: fs.readFileSync(sslConfig.certPath)
}
app.use(express.urlencoded())
app.use(express.static(path.join(__dirname, 'client/')));
const generateSession = async (req, res, next) => {
if (!req.sessionID) {
await req.session.regenerate();
console.log('new session generated with ID =', req.sessionID);
}
next();
}
app.use('/auth', authRouter);
app.use('/api/profile', profileRouter);
app.use('/api/items', itemsRouter);
app.get('/login', generateSession, (req, res)=>{
if (!req.session.backURL){
req.session.backURL = req.header('Referer') || '/';
}
const authUrl = getAuthUrl(req.sessionID);
console.log(`authUrl = ${authUrl}`);
res.redirect(authUrl);
})
app.get('/logout', (req, res)=>{
req.session.regenerate(()=>{
res.redirect('/');
});
})
app.get('*', async (req, res, next) => {
res.sendFile(path.join(__dirname, 'client/index.html'));
})
https.createServer(sslOptions, app).listen(443, () => console.log(`Server started at port 443`));