As shown in the figure below, I have a standalone API project running on a server with say port 3001, and I have a web application running on a server with say port 3002.

The API on port 3001 has all the API routes necessary for a web application (& mobile applications) to retrieve and host data, including authentication APIs (using passport-localand passport-jwt). As a part of the project’s API, I also processed user role authorization, and on each route there is A list of roles that can access the API.
Route Example
todoRoutes.get('/',
requireAuth,
AuthController.roleAuth(['user','editor','admin']),
TodoController.getTodos);
Role Authorization API Method on Port 3001
exports.roleAuth = function(roles){
return function(req, res, next){
var user = req.user;
User.findById(user._id, function(err, foundUser){
if(err){
res.status(422).json({error: 'No user found.'});
return next(err);
}
if(roles.indexOf(foundUser.role) > -1){
return next();
}
res.status(401).json({error: 'You are not authorized to view this content'});
return next('Unauthorized');
});
}
}
json
{
"token": "JWT eyJhbGci...",
"user": {
"_id": "5986b81d940bab06ddc79b34",
"email": "myemail@gmail.com",
"role": "admin"
}
}
-, (), , - , , , , , , .
, :
- - Remote API 3001 ()
- (, ) ()
- , -, loggedin, . API 3002, .