-
Notifications
You must be signed in to change notification settings - Fork 12
feat: primary role #333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: primary role #333
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6a1bfb1
feat: primary role
rakibansary 702585d
ci: deploy to dev
rakibansary a371c6e
debug: test
rakibansary 054d887
fix: include primary-role in signup
rakibansary b73926c
feat: skip onboarding for users with topcoder customer role
rakibansary 59110c6
fix: make labels readable
rakibansary a6213ee
fix: spacing
rakibansary 1f40fe9
fix: sync with auth0 rule
rakibansary 77b5293
feat: set primary role during signup
rakibansary f2647ed
feat: send primary role in payload during user creation
rakibansary 5e08f5e
fix: sync create.js with auth0 config
rakibansary 022de80
feat: skip onboarding for users with topcoder customer role
rakibansary File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
function (user, context, callback) { | ||
function OnboardingChecklist(user, context, callback) { | ||
if (context.clientID === configuration.CLIENT_ACCOUNTS_LOGIN) { | ||
console.log("rule:onboarding-checklist:enter"); | ||
if (context.redirect) { | ||
|
||
if (context.redirect) { | ||
console.log("rule:onboarding-checklist:exiting due to context being a redirect"); | ||
return callback(null, user, context); | ||
} | ||
|
@@ -24,10 +24,17 @@ function (user, context, callback) { | |
const handle = context.idToken[global.AUTH0_CLAIM_NAMESPACE + 'handle']; | ||
console.log("rule:onboarding-checklist: fetch onboarding_checklist for email/handle: ", user.email, handle); | ||
|
||
if (handle == null) { | ||
if (handle === null) { | ||
console.log("rule:onboarding-checklist: exiting due to handle being null."); | ||
return callback(null, user, context); | ||
} | ||
|
||
const roles = context.idToken[global.AUTH0_CLAIM_NAMESPACE + 'roles']; | ||
|
||
if (roles && roles.includes('Topcoder Customer')) { | ||
console.log("rule:onboarding-checklist:exiting due to user being a customer."); | ||
return callback(null, user, context); | ||
} | ||
|
||
const createdAt = _.get(user, "created_at", null); | ||
const thresholdDate = moment(configuration.PROFILE_CREATION_DATE_THRESHOLD, "YYYY-MM-DD"); | ||
|
@@ -42,14 +49,14 @@ function (user, context, callback) { | |
} catch (err) { | ||
console.log("rule:onboarding-checklist: failed to compare userCreationDate", createdAt, " with threshold. Error", err); | ||
} | ||
|
||
/** | ||
* Returns M2M token needed to fetch onboarding_checklist | ||
*/ | ||
const getToken = function(callback) { | ||
const getToken = function (callback) { | ||
if (global.M2MToken) { | ||
console.log('rule:onboarding-checklist:M2M token is available'); | ||
const jwt = require('jsonwebtoken'); | ||
const jwt = require('jsonwebtoken'); | ||
const decoded = jwt.decode(global.M2MToken); | ||
const exp = moment.unix(decoded.exp); | ||
|
||
|
@@ -72,7 +79,7 @@ function (user, context, callback) { | |
} | ||
}, function (err, response, body) { | ||
if (err) { | ||
return callback(err); | ||
return callback(err); | ||
} | ||
|
||
global.M2MToken = body.access_token; | ||
|
@@ -81,83 +88,82 @@ function (user, context, callback) { | |
}); | ||
}; | ||
|
||
getToken(function(err, token) { | ||
getToken(function (err, token) { | ||
if (err) { | ||
console.log('rule:onboarding-checklist:failed to fetch M2M token.'); | ||
return callback(null, user, context); | ||
} | ||
global.AUTH0_CLAIM_NAMESPACE = "https://" + configuration.DOMAIN + "/"; | ||
const axios = require('[email protected]'); | ||
|
||
const options = { | ||
method: 'GET', | ||
url: `https://api.${configuration.DOMAIN}/v5/members/${handle}/traits?traitIds=onboarding_checklist`, | ||
headers: { | ||
Authorization: `Bearer ${token}` | ||
} | ||
}; | ||
|
||
// Fetch onboarding_checklist using v5 member Api. | ||
axios(options) | ||
.then(result => { | ||
try { | ||
const data = result.data; | ||
if (data.length === 0) { | ||
// User doesn't have any traits with traitId onboarding_checklist and should be shown the onboarding wizard | ||
context.idToken[global.AUTH0_CLAIM_NAMESPACE + 'onboarding_wizard'] = 'show'; | ||
console.log('rule:onboarding-checklist:Setting onboarding_wizard to show'); | ||
return callback(null, user, context); | ||
} | ||
.then(result => { | ||
try { | ||
const data = result.data; | ||
|
||
if (data.length === 0) { | ||
// User doesn't have any traits with traitId onboarding_checklist and should be shown the onboarding wizard | ||
context.idToken[global.AUTH0_CLAIM_NAMESPACE + 'onboarding_wizard'] = 'show'; | ||
console.log('rule:onboarding-checklist:Setting onboarding_wizard to show'); | ||
return callback(null, user, context); | ||
} | ||
|
||
const onboardingChecklistTrait = data.filter((item) => item.traitId === 'onboarding_checklist')[0].traits; | ||
let override = 'show'; | ||
for (let checklistTrait of onboardingChecklistTrait.data) { | ||
if (checklistTrait.onboarding_wizard != null) { | ||
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 | ||
checklistTrait.onboarding_wizard.skip ||// for certain signup routes skip is set to true, and thus onboarding wizard needn't be shown | ||
checklistTrait.onboarding_wizard.override === 'skip') | ||
{ | ||
return callback(null, user, context); | ||
} else if (checklistTrait.onboarding_wizard.override === 'useRetUrl') { | ||
override = 'useRetUrl'; | ||
const onboardingChecklistTrait = data.filter((item) => item.traitId === 'onboarding_checklist')[0].traits; | ||
let override = 'show'; | ||
|
||
for (let checklistTrait of onboardingChecklistTrait.data) { | ||
if (checklistTrait.onboarding_wizard !== null) { | ||
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 | ||
checklistTrait.onboarding_wizard.skip ||// for certain signup routes skip is set to true, and thus onboarding wizard needn't be shown | ||
checklistTrait.onboarding_wizard.override === 'skip') { | ||
return callback(null, user, context); | ||
} else if (checklistTrait.onboarding_wizard.override === 'useRetUrl') { | ||
override = 'useRetUrl'; | ||
} | ||
} | ||
} | ||
} | ||
|
||
const profileCompletedData = onboardingChecklistTrait.data.length > 0 ? onboardingChecklistTrait.data[0].profile_completed : null; | ||
|
||
if (profileCompletedData) { | ||
if (profileCompletedData.status === "completed") { | ||
return callback(null, user, context); | ||
} | ||
|
||
for (const item in profileCompletedData.metadata) { | ||
if (profileCompletedData.metadata[item]) { | ||
const profileCompletedData = onboardingChecklistTrait.data.length > 0 ? onboardingChecklistTrait.data[0].profile_completed : null; | ||
|
||
if (profileCompletedData) { | ||
if (profileCompletedData.status === "completed") { | ||
return callback(null, user, context); | ||
} | ||
|
||
for (const item in profileCompletedData.metadata) { | ||
if (profileCompletedData.metadata[item]) { | ||
return callback(null, user, context); | ||
} | ||
} | ||
} | ||
} | ||
|
||
// All checks failed - indicating user newly registered and needs to be shown the onboarding wizard | ||
console.log('rule:onboarding-checklist: set onboarding_wizard ' + override); | ||
|
||
context.idToken[global.AUTH0_CLAIM_NAMESPACE + 'onboarding_wizard'] = override; | ||
|
||
// All checks failed - indicating user newly registered and needs to be shown the onboarding wizard | ||
console.log('rule:onboarding-checklist: set onboarding_wizard ' + override); | ||
|
||
context.idToken[global.AUTH0_CLAIM_NAMESPACE + 'onboarding_wizard'] = override; | ||
|
||
|
||
|
||
return callback(null, user, context); | ||
} catch (e) { | ||
console.log("rule:onboarding-checklist:Error in fetching onboarding_checklist", e); | ||
return callback(null, user, context); | ||
} | ||
}).catch(requestError => { | ||
console.log("rule:onboarding-checklist:Failed fetching onboarding_checklist with error", requestError.response.status); | ||
return callback(null, user, context); | ||
} catch (e) { | ||
console.log("rule:onboarding-checklist:Error in fetching onboarding_checklist", e); | ||
return callback(null, user, context); | ||
} | ||
}).catch(requestError => { | ||
console.log("rule:onboarding-checklist:Failed fetching onboarding_checklist with error", requestError.response.status); | ||
return callback(null, user, context); | ||
}); | ||
}); | ||
}); | ||
} else { | ||
return callback(null, user, context); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / CodeQL
Clear-text logging of sensitive information