Ismar Slomic
Ismar Slomic

Reputation: 5514

Route based on user´s role with ExpressJS Router

I want to call one of two functions for same route based on user´s role.

Route: /api/users

User roles: 1) Admin (authenticated) and 2) Others (collects all other user roles either user is authenticated or not)

Scenario 1 If user has role Admin his GET request to route /api/users should be served by function named getAllUsers()

Scenario 2 If user has role Others his GET request to route /api/users should be served by function named getLimitedUsers()

How would this look like with use of ExpressJS Router?

Upvotes: 1

Views: 641

Answers (1)

Molda
Molda

Reputation: 5704

router.get('/api/users', function(req, res, next){

    if(user.isAdmin) {
        getAllUsers(req, res, next);
    }else{
        getLimitedUsers(req, res, next);
    }

});

function getLimitedUsers(req, res, next){

}

function getAllUsers(req, res, next){

}

Upvotes: 4

Related Questions