0
0
Expressframework~10 mins

Permission middleware in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a middleware function that checks if the user has the required permission.

Express
function checkPermission(permission) {
  return function(req, res, next) {
    if (req.user && req.user.permissions && req.user.permissions.includes([1])) {
      next();
    } else {
      res.status(403).send('Forbidden');
    }
  };
}
Drag options to blanks, or click blank then click option'
Apermissions
Breq.permission
Cpermission
Dreq.user.permission
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.permission instead of the parameter
Checking req.user.permission which is not an array
Using a wrong variable name
2fill in blank
medium

Complete the code to apply the permission middleware to an Express route that requires 'admin' permission.

Express
app.get('/admin', [1]('admin'), (req, res) => {
  res.send('Welcome Admin');
});
Drag options to blanks, or click blank then click option'
AcheckPermission
BauthMiddleware
CrequirePermission
DpermissionCheck
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong middleware function name
Not calling the middleware as a function with the permission argument
3fill in blank
hard

Fix the error in the middleware to properly handle missing user or permissions.

Express
function checkPermission(permission) {
  return function(req, res, next) {
    if ([1]) {
      next();
    } else {
      res.status(403).send('Forbidden');
    }
  };
}
Drag options to blanks, or click blank then click option'
Areq.user && req.user.permissions && permission
Breq.user.permissions
Cpermission
Dreq.user?.permissions?.includes(permission)
Attempts:
3 left
💡 Hint
Common Mistakes
Not checking if req.user exists causing errors
Trying to access permissions without safety checks
4fill in blank
hard

Fill both blanks to create a middleware that logs the permission checked and calls next if allowed.

Express
function checkPermission(permission) {
  return function(req, res, next) {
    console.log('Checking permission:', [1]);
    if (req.user?.permissions?.includes([2])) {
      next();
    } else {
      res.status(403).send('Forbidden');
    }
  };
}
Drag options to blanks, or click blank then click option'
Apermission
Breq.user
Creq.user.permissions
Dreq.permission
Attempts:
3 left
💡 Hint
Common Mistakes
Logging req.user instead of permission
Checking wrong variable for permission
5fill in blank
hard

Fill all three blanks to create a permission middleware that sends a JSON error message when forbidden.

Express
function checkPermission([1]) {
  return function(req, res, next) {
    if (req.user?.permissions?.includes([2])) {
      next();
    } else {
      res.status(403).json({ error: [3] });
    }
  };
}
Drag options to blanks, or click blank then click option'
Apermission
C'Access denied'
D'Forbidden'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names for parameter and check
Sending plain text instead of JSON error
Using incorrect error message string