-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
141 lines (108 loc) · 3.42 KB
/
server.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
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
133
134
135
136
137
138
139
140
141
const jsonServer = require('json-server');
const auth = require('json-server-auth');
const jwt_decode = require('jwt-decode');
// const JWT_SECRET_KEY =
// require('./node_modules/json-server-auth/dist/constants').JWT_SECRET_KEY;
const port = process.env.PORT || 3000;
const server = jsonServer.create();
const router = jsonServer.router('./data/db.json');
const middlewares = jsonServer.defaults();
const rules = auth.rewriter({
/**
* #NOTE: Use custom router with auth here
*/
'/api/*': '/$1',
// Permission rules
users: 600,
// users: 640,
posts: 664,
bookmarks: 600,
postLikes: 664,
// Other rules
// '/posts/:category': '/posts?category=:category',
});
/* end of auth-rules */
const writableMethods = ['POST', 'PUT', 'PATCH', 'DELETE'];
/* end of definitions */
function decodeJWTsID({ req }) {
console.log('decode!');
const token = req.header('Authorization')
? req.header('Authorization').replace('Bearer ', '')
: null;
if (token) {
const decoded = jwt_decode(token);
// console.log({ token, JWT_SECRET_KEY, decoded });
const intSub = Number(decoded.sub);
console.log('subId:::', intSub);
return intSub;
}
/* end of IF-token */
return 0;
}
/* end of decodeJWTsID({ req }) */
function isAdminAuth({ req, res, next }) {
console.log('isAdminAuth!');
const subUserId = decodeJWTsID({ req });
// #REVIEW:
if (subUserId !== 1) {
return res
.status(401)
.jsonp({ message: 'Not A ADMIN!', success: false, status: 401 });
}
/* end of IF-Id */
req.body.userId = subUserId || null;
return next();
}
/* end of isAdminAuth({ req, res, next }) */
/* end of helper-function */
// /!\ Bind the router db to the app server
server.db = router.db;
// Set default middlewares (logger, static, cors and no-cache)
server.use(middlewares);
// To handle POST, PUT and PATCH you need to use a body-parser
// You can use the one used by JSON Server
server.use(jsonServer.bodyParser);
server.use('/api/posts', (req, res, next) => {
console.log('/api/posts!');
const isWritableMethod = writableMethods.includes(req.method);
if (isWritableMethod) {
console.log('Method:::', req.method);
return isAdminAuth({ req, res, next });
}
/* end of IF-(isWritableMethod) */
console.log('isWritableMethod:::', isWritableMethod);
next();
});
/* end of use('/api/posts') */
server.use('/api/*', (req, res, next) => {
console.log('/api/*!');
const isWritableMethod = writableMethods.includes(req.method);
if (isWritableMethod) {
console.log('Method:::', req.method);
// #REVIEWS:
const subUserId = decodeJWTsID({ req });
req.body.userId = subUserId || null;
// req.body.createdAt = Date.now();
req.body.timestamp = Date.now();
}
/* end of IF-(isWritableMethod) */
console.log('isWritableMethod:::', isWritableMethod);
// Continue to JSON Server router
next();
});
/* end of use('/api/*') */
/* end of CUSTOM-use() */
// #REVIEWS: orders of `use()`?
// You must apply the middlewares in the following order
server.use(rules);
// You must apply the auth middleware before the router
server.use(auth);
server.use(router);
/**
* #NOTE: custom router
* BUT unable to use with auth?
*/
// server.use('/api', router);
server.listen(port, () => {
console.log('JSON Server Listening on:' + port);
});