1- import Jane from ' ../lib/jane-service.mjs' ;
2- import apiService from ' ../lib/api-service.mjs' ;
1+ import Jane from " ../lib/jane-service.mjs" ;
2+ import apiService from " ../lib/api-service.mjs" ;
33
4- const AUTH_TRIGGER = ' UserMigration_Authentication' ;
5- const FORGOT_PASSWORD_TRIGGER = ' UserMigration_ForgotPassword' ;
6- const EMAIL_VERIFIED = ' true' ;
7- const CONFIRMED_STATUS = ' CONFIRMED' ;
8- const SUPPRESS_ACTION = ' SUPPRESS' ;
4+ const AUTH_TRIGGER = " UserMigration_Authentication" ;
5+ const FORGOT_PASSWORD_TRIGGER = " UserMigration_ForgotPassword" ;
6+ const EMAIL_VERIFIED = " true" ;
7+ const CONFIRMED_STATUS = " CONFIRMED" ;
8+ const SUPPRESS_ACTION = " SUPPRESS" ;
99
1010const authHandler = async ( event , token ) => {
11- const userExists = await Jane . userExists ( {
12- email : event . userName ,
13- app_client_id : event . callerContext . clientId ,
14- } , token ) ;
11+ const userExists = await Jane . userExists (
12+ {
13+ email : event . userName ,
14+ app_client_id : event . callerContext . clientId ,
15+ } ,
16+ token
17+ ) ;
1518
1619 if ( ! userExists ) {
17- throw new Error ( ' User not found' ) ;
20+ throw new Error ( " User not found" ) ;
1821 }
1922
20- const { valid, errorMessage } = await Jane . verifyCredentials ( {
21- email : event . userName ,
22- password : event . request . password ,
23- } , token ) ;
23+ const { valid, errorMessage, user } = await Jane . verifyCredentials (
24+ {
25+ email : event . userName ,
26+ password : event . request . password ,
27+ user : response . body ?. user ,
28+ } ,
29+ token
30+ ) ;
31+
32+ const attributes = { } ;
33+
34+ if ( user ) {
35+ const { first_name, last_name, phone, birth_date } = user ;
36+
37+ first_name && ( attributes . given_name = first_name ) ;
38+ last_name && ( attributes . family_name = last_name ) ;
39+ phone && ( attributes . phone_number = addAreaCodeToPhone ( phone ) ) ;
40+ birth_date && ( attributes . birthdate = birth_date ) ;
41+ }
2442
2543 if ( valid ) {
2644 event . response . userAttributes = {
2745 email : event . userName ,
2846 email_verified : EMAIL_VERIFIED ,
47+ ...attributes ,
2948 } ;
3049 event . response . finalUserStatus = CONFIRMED_STATUS ;
3150 event . response . messageAction = SUPPRESS_ACTION ;
@@ -37,10 +56,13 @@ const authHandler = async (event, token) => {
3756} ;
3857
3958const forgotPasswordHandler = async ( event , token ) => {
40- const { valid, errorMessage } = await Jane . userCanResetPassword ( {
41- email : event . userName ,
42- app_client_id : event . callerContext . clientId ,
43- } , token ) ;
59+ const { valid, errorMessage } = await Jane . userCanResetPassword (
60+ {
61+ email : event . userName ,
62+ app_client_id : event . callerContext . clientId ,
63+ } ,
64+ token
65+ ) ;
4466
4567 if ( ! valid ) {
4668 throw new Error ( errorMessage ) ;
@@ -62,7 +84,7 @@ export const handler = async (event) => {
6284 ...event ,
6385 request : {
6486 ...event . request ,
65- password : ' REDACTED' ,
87+ password : " REDACTED" ,
6688 } ,
6789 } ;
6890 console . log ( sanitizedEvent ) ;
@@ -75,3 +97,15 @@ export const handler = async (event) => {
7597
7698 return event ;
7799} ;
100+
101+ const addAreaCodeToPhone = ( phone ) => {
102+ let partial = phone . startsWith ( "+" ) ? phone . substring ( 1 ) : phone ;
103+
104+ // Missing + and country code, 2223334444
105+ if ( phone . length === 10 ) {
106+ return `+1${ partial } ` ;
107+ }
108+
109+ // If was already correct, just return the +
110+ return `+${ partial } ` ;
111+ } ;
0 commit comments