Skip to content

Instantly share code, notes, and snippets.

@thameera
Created May 7, 2019 03:48
Show Gist options
  • Save thameera/7a1328a92211da111feed83dd86bf1ae to your computer and use it in GitHub Desktop.
Save thameera/7a1328a92211da111feed83dd86bf1ae to your computer and use it in GitHub Desktop.
Auth0 - add default role to users via rule
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