1- import { mapUserAttributes } from '../lib/utils.mjs' ;
1+ import { addAreaCodeToPhone , mapUserAttributes } from '../lib/utils.mjs' ;
22import Jane from '../lib/jane-service.mjs' ;
33import apiService from '../lib/api-service.mjs' ;
44
@@ -26,12 +26,17 @@ export const handler = async (event) => {
2626 return event ;
2727 }
2828
29- const { success, errorMessage } = await Jane . createUser ( {
30- pool_id : event . userPoolId ,
31- external_id : event . userName ,
32- app_client_id : event . callerContext . clientId ,
33- ...mapUserAttributes ( event . request . userAttributes ) ,
34- } , token ) ;
29+ event = await handleUserMigration ( event , token ) ;
30+
31+ const { success, errorMessage } = await Jane . createUser (
32+ {
33+ pool_id : event . userPoolId ,
34+ external_id : event . userName ,
35+ app_client_id : event . callerContext . clientId ,
36+ ...mapUserAttributes ( event . request . userAttributes ) ,
37+ } ,
38+ token
39+ ) ;
3540
3641 if ( ! success ) {
3742 throw new Error ( `User creation was not successful: ${ errorMessage } ` ) ;
@@ -41,3 +46,88 @@ export const handler = async (event) => {
4146
4247 return event ;
4348} ;
49+ /* Cognito SSO flows do not go through our migration handler
50+ instead we handle those migrations here, after signup.
51+ If a user is signing up via sso, we check for a Jane SSO user
52+ associated with this client and use that users data for the migration */
53+ const handleUserMigration = async ( event , token ) => {
54+ let userIdentities ;
55+ try {
56+ userIdentities = JSON . parse ( event . request . userAttributes . identities ) ;
57+ } catch ( err ) {
58+ console . error ( "userIdentities unable to parse" , err ) ;
59+ return event ;
60+ }
61+
62+ const userGoogleIdentity = userIdentities . find (
63+ ( i ) => i . providerType === "Google"
64+ ) ;
65+ if ( ! userGoogleIdentity ) {
66+ return event ;
67+ }
68+
69+ const { errorMessage, user } = await Jane . verifySSOUser (
70+ {
71+ email : event . request . userAttributes . email ,
72+ user_attributes : event . request . userAttributes ,
73+ app_client_id : event . callerContext . clientId ,
74+ } ,
75+ token
76+ ) ;
77+ if ( errorMessage === "User not found" ) {
78+ // Jane user for this client was not found, continue normal sign up
79+ return event ;
80+ } else if ( errorMessage || ! user ) {
81+ // something went wrong, continue normal sign up and log error
82+ console . error ( `failed to retrieve data for migration: ${ errorMessage } ` ) ;
83+ return event ;
84+ }
85+ const attributes = { } ;
86+ const { first_name, last_name, phone, birth_date } = user ;
87+
88+ const attributesToUpdate = [ ] ;
89+ first_name &&
90+ ( attributes . given_name = first_name ) &&
91+ attributesToUpdate . push ( {
92+ Name : "given_name" ,
93+ Value : first_name ,
94+ } ) ;
95+ last_name &&
96+ ( attributes . family_name = last_name ) &&
97+ attributesToUpdate . push ( {
98+ Name : "family_name" ,
99+ Value : last_name ,
100+ } ) ;
101+ phone &&
102+ ( attributes . phone_number = addAreaCodeToPhone ( phone ) ) &&
103+ attributesToUpdate . push ( {
104+ Name : "phone_number" ,
105+ Value : addAreaCodeToPhone ( phone ) ,
106+ } ) ;
107+ birth_date &&
108+ ( attributes . birthdate = birth_date ) &&
109+ attributesToUpdate . push ( {
110+ Name : "birthdate" ,
111+ Value : birth_date ,
112+ } ) ;
113+ const cognitoIdServiceProvider = new CognitoIdentityProviderClient ( {
114+ region : "us-east-1" ,
115+ } ) ;
116+ const command = new AdminUpdateUserAttributesCommand ( {
117+ UserAttributes : attributesToUpdate ,
118+ UserPoolId : event . userPoolId ,
119+ Username : event . userName ,
120+ } ) ;
121+ await cognitoIdServiceProvider
122+ . send ( command )
123+ . then ( ( data ) => console . log ( "Cognito user updated!" , data ) )
124+ . catch ( ( err ) => {
125+ console . error ( "Cognito Attribute Update Unsuccessful" , err ) ;
126+ } ) ;
127+
128+ event . request . userAttributes = {
129+ ...event . request . userAttributes ,
130+ ...attributes ,
131+ } ;
132+ return event ;
133+ } ;
0 commit comments