Skip to content

Instantly share code, notes, and snippets.

@bchiang7
Created November 8, 2019 02:59
Show Gist options
  • Save bchiang7/186cf1a859c09b9af4eaea1bf8422d06 to your computer and use it in GitHub Desktop.
Save bchiang7/186cf1a859c09b9af4eaea1bf8422d06 to your computer and use it in GitHub Desktop.
/**
* Check if a user has access to the app admin
*/
const isAdmin = async (req, res, next) => {
if (req.headers && req.headers.authorization) {
if (!req.user || !req.user.isAdmin) {
return res.status(403).json({ message: 'You must have admin permissions.' });
}
// If we get here, we're good so pass it along
next();
} else {
const err = new Error('No auth headers set. You must have admin permissions.');
// Pass the error to error handling middleware functions
next(err);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment