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