-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
38 lines (35 loc) · 1.1 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
const { send } = require('micro');
const users = require('./src/services/user.service');
const auth = require('./src/authentication/authentication');
const db = require('./src/models/db');
module.exports = async function(req, res) {
res.setHeader('Content-Type', 'application/json');
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Credentials', 'true');
res.setHeader(
'Access-Control-Allow-Methods',
'GET,HEAD,OPTIONS,POST,PUT,DELETE'
);
res.setHeader(
'Access-Control-Allow-Headers',
'Authorization, Access-Control-Allow-Headers, Access-Control-Allow-Methods, Origin,Accept, X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers'
);
if (req.method == 'OPTIONS') {
return '';
}
try {
switch (req.url) {
case '/api/authentication':
return auth.login(req, res);
case '/api/users':
if (auth.decode(req, res) !== null) {
send(res, 200, await users.list());
}
break;
default:
break;
}
} catch (err) {
send(res, 500, err);
}
};