Skip to content

Instantly share code, notes, and snippets.

@holmesal
Created November 30, 2016 22:09
Show Gist options
  • Save holmesal/af9cd79f664ddf9883ce4b9d53e019b0 to your computer and use it in GitHub Desktop.
Save holmesal/af9cd79f664ddf9883ce4b9d53e019b0 to your computer and use it in GitHub Desktop.
export const requireAuth = (target, name, descriptor) => {
const resolver = target[name].resolve;
target[name].resolve = (user, args, context) => {
const { user: authUser } = context;
// If there exists an authUser, then allow the read
if (authUser) {
return resolver(user, args, context);
} else {
// this field will resolve to null for unauthenticated users
return null;
}
};
};
export const requireSameAuthUser = (target, name, descriptor) => {
const resolver = target[name].resolve;
target[name].resolve = (user, args, context) => {
const { user: authUser } = context;
// If this is the owner, allow the read
if (authUser && user.id === authUser.id) {
return resolver(user, args, context);
} else {
// this field will resolve to null for all other viewers
return null;
}
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment