summaryrefslogtreecommitdiffstats
path: root/backend/src/utils.js
diff options
context:
space:
mode:
Diffstat (limited to 'backend/src/utils.js')
-rw-r--r--backend/src/utils.js19
1 files changed, 18 insertions, 1 deletions
diff --git a/backend/src/utils.js b/backend/src/utils.js
index 642c04a..2988b3d 100644
--- a/backend/src/utils.js
+++ b/backend/src/utils.js
@@ -7,8 +7,24 @@ function getUserId(ctx) {
const { userId } = jwt.verify(token, process.env.APP_SECRET);
return userId;
}
+ // TODO: Don't throw when they aren't logged in
+ // throw new Error('Sorry, you must be logged in to do that!');
+}
+
+function hasPermission(user, permissionsNeeded) {
+ const matchedPermissions = user.permissions.filter(permissionTheyHave =>
+ permissionsNeeded.includes(permissionTheyHave)
+ );
+ if (!matchedPermissions.length) {
+ throw new Error(`You do not have sufficient permissions
+
+ : ${permissionsNeeded}
- throw new AuthError();
+ You Have:
+
+ ${user.permissions}
+ `);
+ }
}
function checkForUserId(ctx) {
@@ -30,4 +46,5 @@ module.exports = {
getUserId,
AuthError,
checkForUserId,
+ hasPermission,
};