Skip to content

Instantly share code, notes, and snippets.

@marekpiechut
Last active January 21, 2019 12:56
Show Gist options
  • Select an option

  • Save marekpiechut/0541bd2779ea30b332aab48b34766829 to your computer and use it in GitHub Desktop.

Select an option

Save marekpiechut/0541bd2779ea30b332aab48b34766829 to your computer and use it in GitHub Desktop.
Authentication and Authorization in NodeJS GraphQL API
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