-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth0rule.js
38 lines (29 loc) · 1.03 KB
/
auth0rule.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// This scoops data out of the auth0 rules and puts into the JWT so we can use it.
function (user, context, callback) {
const namespace = 'https://carpal.org.au';
const assignedRoles = (context.authorization || {}).roles;
let idTokenClaims = context.idToken || {};
let accessTokenClaims = context.accessToken || {};
function setClaim(claimId, value) {
idTokenClaims[`${namespace}/${claimId}`] = value;
accessTokenClaims[`${namespace}/${claimId}`] = value;
}
setClaim("roles", assignedRoles);
const roleLookup = assignedRoles.reduce((soFar, role) => {
soFar[role] = true;
return soFar;
}, {});
if (roleLookup.male) {
setClaim("gender", "male");
} else if (roleLookup.female) {
setClaim("gender", "female");
}
if (roleLookup.suv) {
setClaim("car", "suv");
} else {
setClaim("car", "noSUV");
}
context.idToken = idTokenClaims;
context.accessToken = accessTokenClaims;
callback(null, user, context);
}