Created
November 30, 2016 22:09
-
-
Save holmesal/af9cd79f664ddf9883ce4b9d53e019b0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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