11function ( user , context , callback ) {
22 if ( context . clientID === configuration . CLIENT_ACCOUNTS_LOGIN ) {
3+ console . log ( "rule:onboarding-checklist:enter" ) ;
4+
35 if ( context . redirect ) {
6+ console . log ( "rule:onboarding-checklist:exiting due to context being a redirect" ) ;
47 return callback ( null , user , context ) ;
58 }
69
10+ const _ = require ( 'lodash' ) ;
11+
712 let handle = _ . get ( user , "handle" , null ) ;
813 const provider = _ . get ( user , "identities[0].provider" , null ) ;
914 if ( ! handle && provider === "auth0" ) {
1015 handle = _ . get ( user , "nickname" , null ) ;
1116 }
12- console . log ( "Fetch onboarding_checklist for email/handle: " , user . email , handle , provider ) ;
1317
14- global . AUTH0_CLAIM_NAMESPACE = "https://" + configuration . DOMAIN + "/" ;
15- try {
16- const axios = require ( 'axios@0.19.2' ) ;
17-
18- const redirectUrl = `https://platform.${ configuration . DOMAIN } /onboard` ;
19- const options = {
20- method : 'GET' ,
21- url : `https://api.${ configuration . DOMAIN } /v5/members/${ handle } /traits?traitIds=onboarding_checklist` ,
18+ const loginCount = _ . get ( context , "stats.loginsCount" ) ;
19+ console . log ( 'rule:onboarding-checklist: loginCount' , loginCount ) ;
20+
21+ const getToken = function ( callback ) {
22+ if ( global . M2MToken ) {
23+ console . log ( 'rule:onboarding-checklist:M2M token is available' ) ;
24+ const jwt = require ( 'jsonwebtoken' ) ;
25+ const moment = require ( 'moment' ) ;
26+ const decoded = jwt . decode ( global . M2MToken ) ;
27+ const exp = moment . unix ( decoded . exp ) ;
28+
29+ if ( exp > new Date ( ) ) {
30+ console . log ( 'rule:onboarding-checklist:M2M token is valid. Reusing...' ) ;
31+ return callback ( null , global . M2MToken ) ;
32+ }
33+ }
34+
35+ console . log ( 'rule:onboarding-checklist:Fetching new M2M token' ) ;
36+ request . post ( {
37+ url : `https://auth0proxy.${ configuration . DOMAIN } /token` ,
38+ headers : 'content-type: application/json' ,
39+ json : {
40+ client_id : configuration . M2M_CLIENT_ID ,
41+ client_secret : configuration . M2M_CLIENT_SECRET ,
42+ audience : configuration . M2M_AUDIENCE ,
43+ auth0_url : configuration . M2M_TOKEN_URL ,
44+ grant_type : 'client_credentials'
45+ }
46+ } , function ( err , response , body ) {
47+ if ( err ) {
48+ return callback ( err ) ;
49+ }
50+
51+ global . M2MToken = body . access_token ;
52+ console . log ( 'rule:onboarding-checklist:setting the M2MToken globally' ) ;
53+ return callback ( null , global . M2MToken ) ;
54+ } ) ;
55+ }
56+
57+ getToken ( function ( err , token ) {
58+ if ( err ) {
59+ console . log ( 'rule:onboarding-checklist:failed to fetch M2M token.' ) ;
60+ return callback ( null , user , context ) ;
2261 }
62+
63+ console . log ( "rule:onboarding-checklist: fetch onboarding_checklist for email/handle: " , user . email , handle , provider ) ;
64+ global . AUTH0_CLAIM_NAMESPACE = "https://" + configuration . DOMAIN + "/" ;
65+ try {
66+ const axios = require ( 'axios@0.19.2' ) ;
2367
24- axios ( options ) . then ( result => {
25- const data = result . data ;
26-
27- if ( data . length === 0 ) {
28- context . redirect = {
29- url : redirectUrl
68+ const redirectUrl = `https://platform. ${ configuration . DOMAIN } /onboard` ;
69+ const options = {
70+ method : 'GET' ,
71+ url : `https://api. ${ configuration . DOMAIN } /v5/members/ ${ handle } /traits?traitIds=onboarding_checklist` ,
72+ headers : {
73+ Authorization : `Bearer ${ token } `
3074 }
31- console . log ( 'Setting redirectUrl' , redirectUrl ) ;
32- return callback ( null , user , context ) ;
3375 }
34-
35- const onboardingChecklistTrait = data . filter ( ( item ) => item . traitId === 'onboarding_checklist' ) [ 0 ] . traits ;
36-
37- for ( let checklistTrait of onboardingChecklistTrait . data ) {
38- if (
39- checklistTrait . onboarding_wizard != null &&
40- ( checklistTrait . onboarding_wizard . status != null ||
41- checklistTrait . onboarding_wizard . skip )
42- ) {
43- return callback ( null , user , context ) ;
76+
77+ axios ( options ) . then ( result => {
78+ const data = result . data ;
79+
80+ if ( data . length === 0 ) {
81+ context . redirect = {
82+ url : redirectUrl
4483 }
45- }
46-
47- const profileCompletedData = onboardingChecklistTrait . data [ 0 ] . profile_completed ;
48-
49- if ( profileCompletedData ) {
50- if ( profileCompletedData . status === "completed" ) {
84+ console . log ( 'rule:onboarding-checklist:Setting redirectUrl' , redirectUrl ) ;
5185 return callback ( null , user , context ) ;
5286 }
53-
54- for ( const item in profileCompletedData . metadata ) {
55- console . log ( 'checking item' , item ) ;
56- if ( profileCompletedData . metadata [ item ] ) {
87+
88+ const onboardingChecklistTrait = data . filter ( ( item ) => item . traitId === 'onboarding_checklist' ) [ 0 ] . traits ;
89+
90+ for ( let checklistTrait of onboardingChecklistTrait . data ) {
91+ if (
92+ checklistTrait . onboarding_wizard != null &&
93+ ( checklistTrait . onboarding_wizard . status != null ||
94+ checklistTrait . onboarding_wizard . skip )
95+ ) {
96+ return callback ( null , user , context ) ;
97+ }
98+ }
99+
100+ const profileCompletedData = onboardingChecklistTrait . data [ 0 ] . profile_completed ;
101+
102+ if ( profileCompletedData ) {
103+ if ( profileCompletedData . status === "completed" ) {
57104 return callback ( null , user , context ) ;
58105 }
106+
107+ for ( const item in profileCompletedData . metadata ) {
108+ if ( profileCompletedData . metadata [ item ] ) {
109+ return callback ( null , user , context ) ;
110+ }
111+ }
59112 }
60- }
61-
62- context . redirect = {
63- url : redirectUrl
64- }
65- console . log ( 'Setting redirectUrl' , redirectUrl ) ;
113+
114+ context . redirect = {
115+ url : redirectUrl
116+ }
117+ console . log ( 'rule:onboarding-checklist:Setting redirectUrl' , redirectUrl ) ;
118+ return callback ( null , user , context ) ;
119+ } )
120+
121+ } catch ( e ) {
122+ console . log ( "rule:onboarding-checklist:Error in fetching onboarding_checklist" , + e ) ;
66123 return callback ( null , user , context ) ;
67- } )
68-
69- } catch ( err ) {
70- console . log ( "Error in fetching onboarding_checklist" , + err ) ;
71- return callback ( null , user , context ) ;
72- }
124+ }
125+ } )
73126 } else {
74127 return callback ( null , user , context ) ;
75128 }
76- }
129+ }
0 commit comments