Repository navigation
Expand file tree
/
Copy pathapp.ts
More file actions
114 lines (94 loc) · 2.73 KB
/
Copy pathapp.ts
File metadata and controls
114 lines (94 loc) · 2.73 KB
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
import express from 'express';
import morgan from 'morgan';
import cors from 'cors';
import path from "path";
import session from 'express-session';
import apiRouter from './routes/api';
import configuration from './common/configuration';
import prisma from './common/client';
import { OidcUser } from 'common/types';
const app = express();
app.use(cors());
app.use(express.json());
app.use(session({
secret: configuration.SESSION_SECRET,
resave: false,
saveUninitialized: false,
}));
let client: any;
async function initOIDC() {
const { Issuer } = await import('openid-client');
const issuer = await Issuer.discover(configuration.OIDC_ISSUER!);
client = new issuer.Client({
client_id: configuration.OIDC_CLIENT_ID,
client_secret: configuration.OIDC_CLIENT_SECRET,
redirect_uris: [configuration.REDIRECT_URL],
});
console.log('OIDC client initialized');
}
initOIDC();
app.get('/login', (req, res) => {
const authUrl = client.authorizationUrl({
scope: 'openid profile email permissions',
});
res.redirect(authUrl);
});
// Callback
app.get('/oidc/callback', async (req, res, next) => {
try {
const params = client.callbackParams(req);
const tokenSet = await client.callback(
configuration.REDIRECT_URL,
params
);
const user = await client.userinfo(tokenSet.access_token)
req.session.user = user
res.redirect('/');
} catch (err) {
next(err);
}
});
// Logout
app.get('/logout', (req, res) => {
req.session.destroy(() => {
res.redirect('/');
});
});
app.use((req, res, next) => {
next();
})
setInterval(() => {
(async () => {
const cases = await prisma.case.findMany();
const toDelete = [];
for (const c of cases) {
if (c.delete && c.delete?.getTime() > Date.now()) {
toDelete.push(c.id)
}
}
for (const id of toDelete) {
prisma.case.delete({
where: {
id
}
})
.then(() => {
console.log(`Deleted ${id}`)
})
}
})()
}, 3600 * 1000)
// Log requests to console
// Don't log when NODE_ENV == testing
if (configuration.NODE_ENV === "development") {
app.use(morgan("dev"));
} else if (configuration.NODE_ENV === "production") {
app.use(morgan("common"));
}
app.use("/api", apiRouter);
// Serve React app
app.use(express.static("./client/build"));
app.get("*", (req, res) => res.sendFile(path.resolve(__dirname, "client", "build", "index.html")))
const PORT = configuration.PORT;
app.listen(PORT, () => console.log(`Listening on port ${PORT}`));
export default app;