-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
54 lines (47 loc) · 1.44 KB
/
app.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
import { app } from 'mu';
import login from './routes/login';
import register from './routes/register';
import current from './routes/current';
import logout from './routes/logout';
import deleteCurrentAccount from './routes/deleteCurrentAccount';
import changePassword from './routes/changePassword';
app.use('*', (req, res, next) => {
if (!req.is('application/vnd.api+json')) return res.status(400).json({
"errors": [
{
"status": "400",
"detail": "Content-Type has to be set to 'application/vnd.api+json'"
}
]
});
if (!req.headers['mu-session-id']) return res.status(400).json({
"errors": [
{
"status": "400",
"detail": "mu-session-id header is missing'"
}
]
});
next();
});
/**
* Higher order caller function for global error handling.
* this will catch errors, alternative for try/catch
*/
const use = fn => (req, res, next) => {
Promise.resolve(fn(req, res, next)).catch(next);
};
app.post('/sessions', use(login));
app.post('/accounts', use(register));
app.post('/sessions', use(login));
app.get('/sessions/current', use(current));
app.delete('/sessions/current', use(logout));
app.delete('/accounts/current', use(deleteCurrentAccount));
app.patch('/accounts/current/changePassword', use(changePassword));
// Error handling middleware
app.use(function(err, req, res, next){
console.log(err);
res.status(500).json({
errors: [{ title: "Something went wrong" }]
});
});