Created
May 7, 2019 03:48
-
-
Save thameera/7a1328a92211da111feed83dd86bf1ae to your computer and use it in GitHub Desktop.
Auth0 - add default role to users via rule
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
async function (user, context, callback) { | |
// Ensure every user has the user role | |
if (context.authorization.roles.length === 0) { | |
console.log(`No roles for the user ${user.user_id}. Assigning default role`); | |
try { | |
const request = require('request-promise'); | |
await request.post(`${auth0.baseUrl}/users/${user.user_id}/roles`, { | |
body: { | |
"roles": ["rol_6KcsnzJ7Bd4YZeG1"] | |
}, | |
auth: { | |
'bearer': auth0.accessToken | |
}, | |
json: true | |
}); | |
} catch (error) { | |
console.log(error); | |
} | |
} else { | |
console.log('User already has a role'); | |
} | |
callback(null, user, context); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment