Skip to content

Instantly share code, notes, and snippets.

@Tymek
Last active December 28, 2019 20:38
Show Gist options
  • Save Tymek/3e86e9520ca3e3444a48196eb92452ef to your computer and use it in GitHub Desktop.
Save Tymek/3e86e9520ca3e3444a48196eb92452ef to your computer and use it in GitHub Desktop.
Auth0 rule for JWT Hasura roles
function (user, context, callback) {
// console.log('context', context);
const namespace = "https://hasura.io/jwt/claims";
const roles = (context.authorization || {}).roles || [];
const claims = {
'x-hasura-default-role': 'user',
'x-hasura-allowed-roles': [
'anonymous',
'user',
...roles,
],
'x-hasura-user-id': user.user_id
};
context.accessToken[namespace] = claims;
context.idToken[namespace] = claims;
// console.log('claims', claims);
callback(null, user, context);
}
@Tymek
Copy link
Author

Tymek commented Dec 28, 2019

Sources:
Auth0 Docs, Rules with Authorization: Add user roles to tokens
Hasura Docs, Auth0 JWT Integration with Hasura GraphQL engine: Configure Auth0 Rules & Callback URLs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment