I have 2 authentication systems. They was implemented in auth1 middleware, and auth2 middleware. They look like:
// service1 for auth1 and service2 for auth2
let authInfo = service1.check(username, password);
if (authInfo) {
req.user = authInfo;
}
next();
For some routes need only auth1 as following example.
router.post('/staff', auth1, newStaff);
For some routes need only auth2 as following example.
router.post('/user', auth2, newUser);
For some route need both of them.
router.post('/vip', auth1, auth2, newVip);
I tried to add another middleware called authCheck to above routes like the following code. It works fine.
router.post('/staff', auth1, authCheck, newStaff);
router.post('/user', auth2, authCheck, newUser);
router.post('/vip', auth1, auth2, authCheck, newVip);
In authCheck looks like the following code.
if (req.user) {
next();
}
else {
res.status(401).end();
}
Is it the proper way to deal with 2 authentication systems? Can I avoid the authCheck middleware?