Skip to content

Commit 1f40fe9

Browse files
committed
fix: sync with auth0 rule
Signed-off-by: Rakib Ansary <[email protected]>
1 parent a6213ee commit 1f40fe9

File tree

1 file changed

+62
-63
lines changed

1 file changed

+62
-63
lines changed
Lines changed: 62 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
function (user, context, callback) {
1+
function OnboardingChecklist(user, context, callback) {
22
if (context.clientID === configuration.CLIENT_ACCOUNTS_LOGIN) {
33
console.log("rule:onboarding-checklist:enter");
4-
5-
if (context.redirect) {
4+
5+
if (context.redirect) {
66
console.log("rule:onboarding-checklist:exiting due to context being a redirect");
77
return callback(null, user, context);
88
}
@@ -24,15 +24,15 @@ function (user, context, callback) {
2424
const handle = context.idToken[global.AUTH0_CLAIM_NAMESPACE + 'handle'];
2525
console.log("rule:onboarding-checklist: fetch onboarding_checklist for email/handle: ", user.email, handle);
2626

27-
const roles = context.idToken[global.AUTH0_CLAIM_NAMESPACE + 'roles']
28-
29-
if (roles && roles.includes('Topcoder Customer')) {
30-
console.log("rule:onboarding-checklist:exiting due to user being a customer.");
27+
if (handle === null) {
28+
console.log("rule:onboarding-checklist: exiting due to handle being null.");
3129
return callback(null, user, context);
3230
}
31+
32+
const roles = context.idToken[global.AUTH0_CLAIM_NAMESPACE + 'roles'];
3333

34-
if (handle == null) {
35-
console.log("rule:onboarding-checklist: exiting due to handle being null.");
34+
if (roles && roles.includes('Topcoder Customer')) {
35+
console.log("rule:onboarding-checklist:exiting due to user being a customer.");
3636
return callback(null, user, context);
3737
}
3838

@@ -49,14 +49,14 @@ function (user, context, callback) {
4949
} catch (err) {
5050
console.log("rule:onboarding-checklist: failed to compare userCreationDate", createdAt, " with threshold. Error", err);
5151
}
52-
52+
5353
/**
5454
* Returns M2M token needed to fetch onboarding_checklist
5555
*/
56-
const getToken = function(callback) {
56+
const getToken = function (callback) {
5757
if (global.M2MToken) {
5858
console.log('rule:onboarding-checklist:M2M token is available');
59-
const jwt = require('jsonwebtoken');
59+
const jwt = require('jsonwebtoken');
6060
const decoded = jwt.decode(global.M2MToken);
6161
const exp = moment.unix(decoded.exp);
6262

@@ -79,7 +79,7 @@ function (user, context, callback) {
7979
}
8080
}, function (err, response, body) {
8181
if (err) {
82-
return callback(err);
82+
return callback(err);
8383
}
8484

8585
global.M2MToken = body.access_token;
@@ -88,83 +88,82 @@ function (user, context, callback) {
8888
});
8989
};
9090

91-
getToken(function(err, token) {
91+
getToken(function (err, token) {
9292
if (err) {
9393
console.log('rule:onboarding-checklist:failed to fetch M2M token.');
9494
return callback(null, user, context);
9595
}
9696
global.AUTH0_CLAIM_NAMESPACE = "https://" + configuration.DOMAIN + "/";
9797
const axios = require('[email protected]');
98-
98+
9999
const options = {
100100
method: 'GET',
101101
url: `https://api.${configuration.DOMAIN}/v5/members/${handle}/traits?traitIds=onboarding_checklist`,
102102
headers: {
103103
Authorization: `Bearer ${token}`
104104
}
105105
};
106-
106+
107107
// Fetch onboarding_checklist using v5 member Api.
108108
axios(options)
109-
.then(result => {
110-
try {
111-
const data = result.data;
112-
113-
if (data.length === 0) {
114-
// User doesn't have any traits with traitId onboarding_checklist and should be shown the onboarding wizard
115-
context.idToken[global.AUTH0_CLAIM_NAMESPACE + 'onboarding_wizard'] = 'show';
116-
console.log('rule:onboarding-checklist:Setting onboarding_wizard to show');
117-
return callback(null, user, context);
118-
}
109+
.then(result => {
110+
try {
111+
const data = result.data;
112+
113+
if (data.length === 0) {
114+
// User doesn't have any traits with traitId onboarding_checklist and should be shown the onboarding wizard
115+
context.idToken[global.AUTH0_CLAIM_NAMESPACE + 'onboarding_wizard'] = 'show';
116+
console.log('rule:onboarding-checklist:Setting onboarding_wizard to show');
117+
return callback(null, user, context);
118+
}
119119

120-
const onboardingChecklistTrait = data.filter((item) => item.traitId === 'onboarding_checklist')[0].traits;
121-
let override = 'show';
122-
123-
for (let checklistTrait of onboardingChecklistTrait.data) {
124-
if (checklistTrait.onboarding_wizard != null) {
125-
if ( checklistTrait.onboarding_wizard.status !== 'pending_at_user' || // any non pending_at_user status indicates OB was either seen or completed and can be skipped
126-
checklistTrait.onboarding_wizard.skip ||// for certain signup routes skip is set to true, and thus onboarding wizard needn't be shown
127-
checklistTrait.onboarding_wizard.override === 'skip')
128-
{
129-
return callback(null, user, context);
130-
} else if (checklistTrait.onboarding_wizard.override === 'useRetUrl') {
131-
override = 'useRetUrl';
120+
const onboardingChecklistTrait = data.filter((item) => item.traitId === 'onboarding_checklist')[0].traits;
121+
let override = 'show';
122+
123+
for (let checklistTrait of onboardingChecklistTrait.data) {
124+
if (checklistTrait.onboarding_wizard !== null) {
125+
if (checklistTrait.onboarding_wizard.status !== 'pending_at_user' || // any non pending_at_user status indicates OB was either seen or completed and can be skipped
126+
checklistTrait.onboarding_wizard.skip ||// for certain signup routes skip is set to true, and thus onboarding wizard needn't be shown
127+
checklistTrait.onboarding_wizard.override === 'skip') {
128+
return callback(null, user, context);
129+
} else if (checklistTrait.onboarding_wizard.override === 'useRetUrl') {
130+
override = 'useRetUrl';
131+
}
132132
}
133133
}
134-
}
135134

136-
const profileCompletedData = onboardingChecklistTrait.data.length > 0 ? onboardingChecklistTrait.data[0].profile_completed : null;
137-
138-
if (profileCompletedData) {
139-
if (profileCompletedData.status === "completed") {
140-
return callback(null, user, context);
141-
}
142-
143-
for (const item in profileCompletedData.metadata) {
144-
if (profileCompletedData.metadata[item]) {
135+
const profileCompletedData = onboardingChecklistTrait.data.length > 0 ? onboardingChecklistTrait.data[0].profile_completed : null;
136+
137+
if (profileCompletedData) {
138+
if (profileCompletedData.status === "completed") {
145139
return callback(null, user, context);
146140
}
141+
142+
for (const item in profileCompletedData.metadata) {
143+
if (profileCompletedData.metadata[item]) {
144+
return callback(null, user, context);
145+
}
146+
}
147147
}
148-
}
149-
150-
// All checks failed - indicating user newly registered and needs to be shown the onboarding wizard
151-
console.log('rule:onboarding-checklist: set onboarding_wizard ' + override);
152-
153-
context.idToken[global.AUTH0_CLAIM_NAMESPACE + 'onboarding_wizard'] = override;
154148

149+
// All checks failed - indicating user newly registered and needs to be shown the onboarding wizard
150+
console.log('rule:onboarding-checklist: set onboarding_wizard ' + override);
155151

152+
context.idToken[global.AUTH0_CLAIM_NAMESPACE + 'onboarding_wizard'] = override;
156153

154+
155+
156+
return callback(null, user, context);
157+
} catch (e) {
158+
console.log("rule:onboarding-checklist:Error in fetching onboarding_checklist", e);
159+
return callback(null, user, context);
160+
}
161+
}).catch(requestError => {
162+
console.log("rule:onboarding-checklist:Failed fetching onboarding_checklist with error", requestError.response.status);
157163
return callback(null, user, context);
158-
} catch (e) {
159-
console.log("rule:onboarding-checklist:Error in fetching onboarding_checklist", e);
160-
return callback(null, user, context);
161-
}
162-
}).catch(requestError => {
163-
console.log("rule:onboarding-checklist:Failed fetching onboarding_checklist with error", requestError.response.status);
164-
return callback(null, user, context);
165-
});
164+
});
166165
});
167166
} else {
168167
return callback(null, user, context);
169168
}
170-
}
169+
}

0 commit comments

Comments
 (0)