Skip to content

Commit 19e5b90

Browse files
committed
update middleware
1 parent 6d348af commit 19e5b90

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

server.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,12 @@ app.use(bodyParser.urlencoded({ extended: true }));
5252
app.use(cookieParser(process.env.COOKIE_SECRET));
5353

5454

55-
//middleware that checks if JWT token exists and verifies it if it does exist.
56-
//In all future routes, this helps to know if the request is authenticated or not.
57-
app.use(function (req, res, next) {
55+
// middleware that checks if JWT token exists and verifies it if it does exist.
56+
// In all private routes, this helps to know if the request is authenticated or not.
57+
const authMiddleware = function (req, res, next) {
5858
// check header or url parameters or post parameters for token
5959
var token = req.headers['authorization'];
60-
if (!token) return next(); //if no token, continue
60+
if (!token) return handleResponse(req, res, 401);
6161

6262
token = token.replace('Bearer ', '');
6363

@@ -83,7 +83,7 @@ app.use(function (req, res, next) {
8383
next();
8484
}
8585
});
86-
});
86+
}
8787

8888

8989
// validate user credentials
@@ -183,10 +183,7 @@ app.post('/verifyToken', function (req, res) {
183183

184184

185185
// get list of the users
186-
app.get('/users/getList', (req, res) => {
187-
if (!req.user)
188-
return handleResponse(req, res, 401);
189-
186+
app.get('/users/getList', authMiddleware, (req, res) => {
190187
const list = userList.map(x => {
191188
const user = { ...x };
192189
delete user.password;

0 commit comments

Comments
 (0)