Last active
January 21, 2019 12:56
-
-
Save marekpiechut/0541bd2779ea30b332aab48b34766829 to your computer and use it in GitHub Desktop.
Authentication and Authorization in NodeJS GraphQL API
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
| const requiresRole = role => resolver => { | |
| return (parent, args, context, info) => { | |
| if (context.user && (!role || context.user.role === role)) { | |
| return resolver(parent, args, context, info) | |
| } else { | |
| throw new AuthenticationError('Unauthorized') | |
| } | |
| } | |
| } | |
| const membersOnly = requiresRole('MEMBER') | |
| const adminsOnly = requiresRole('ADMIN') | |
| const requiresLogin = requiresRole(null) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment