Last active
December 28, 2019 20:38
-
-
Save Tymek/3e86e9520ca3e3444a48196eb92452ef to your computer and use it in GitHub Desktop.
Auth0 rule for JWT Hasura roles
This file contains 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
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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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