From 26c68929311cd7962a5a1a9802c2c9d320007a24 Mon Sep 17 00:00:00 2001 From: Hoang Nguyen Date: Wed, 16 Oct 2024 18:06:57 -0700 Subject: [PATCH] fix: auth codegen to include phone verification setting --- .../src/auth_render_adapter.test.ts | 14 ++++++ .../src/auth_render_adapter.ts | 8 ++++ .../src/auth/source_builder.test.ts | 13 +++++ .../src/auth/source_builder.ts | 47 ++++++++++++++++--- 4 files changed, 76 insertions(+), 6 deletions(-) diff --git a/packages/amplify-gen1-codegen-auth-adapter/src/auth_render_adapter.test.ts b/packages/amplify-gen1-codegen-auth-adapter/src/auth_render_adapter.test.ts index 729140c0f40..451846c54e0 100644 --- a/packages/amplify-gen1-codegen-auth-adapter/src/auth_render_adapter.test.ts +++ b/packages/amplify-gen1-codegen-auth-adapter/src/auth_render_adapter.test.ts @@ -426,6 +426,20 @@ void describe('auth codegen', () => { }); }); }); + + void describe('phone settings', () => { + void it('sets phone verification message', () => { + const result = getAuthDefinition({ + userPool: { + SmsVerificationMessage: 'Your sms verification code is {####}.', + }, + }); + assert.deepEqual(result.loginOptions?.phone, { + verificationMessage: 'Your sms verification code is {####}.', + }); + }); + }); + void describe('Email verification settings', () => { void it('it sets email verification with code message', () => { const emailMessage = 'Your verification code is {####}'; diff --git a/packages/amplify-gen1-codegen-auth-adapter/src/auth_render_adapter.ts b/packages/amplify-gen1-codegen-auth-adapter/src/auth_render_adapter.ts index 185d2e58a42..f64bd6465bf 100644 --- a/packages/amplify-gen1-codegen-auth-adapter/src/auth_render_adapter.ts +++ b/packages/amplify-gen1-codegen-auth-adapter/src/auth_render_adapter.ts @@ -331,9 +331,17 @@ export const getAuthDefinition = ({ if (userPool.UsernameAttributes?.includes('phone_number')) { loginWith.phone = true; } + if (userPool.EmailVerificationMessage || userPool.EmailVerificationSubject) { loginWith.emailOptions = getEmailConfig(userPool); } + + if (userPool.SmsVerificationMessage) { + loginWith.phone = { + verificationMessage: userPool.SmsVerificationMessage, + }; + } + if (webClient?.CallbackURLs) { loginWith.callbackURLs = webClient?.CallbackURLs; } diff --git a/packages/amplify-gen2-codegen/src/auth/source_builder.test.ts b/packages/amplify-gen2-codegen/src/auth/source_builder.test.ts index b130bb9c03a..9343b621aa4 100644 --- a/packages/amplify-gen2-codegen/src/auth/source_builder.test.ts +++ b/packages/amplify-gen2-codegen/src/auth/source_builder.test.ts @@ -332,6 +332,19 @@ describe('render auth node', () => { }); }); describe('phone', () => { + it('renders phone options', () => { + const authDefinition: AuthDefinition = { + loginOptions: { + phone: { + verificationMessage: 'My Verification Message', + }, + }, + }; + const node = renderAuthNode(authDefinition); + const source = printNodeArray(node); + assert.match(source, /phone:\s*{\s*verificationMessage:\s*\(\)\s*=>\s*"My Verification Message"\s*}/); + }); + it('renders `phone: true`', () => { const authDefinition: AuthDefinition = { loginOptions: { diff --git a/packages/amplify-gen2-codegen/src/auth/source_builder.ts b/packages/amplify-gen2-codegen/src/auth/source_builder.ts index 908796c2422..9e17177dc8c 100644 --- a/packages/amplify-gen2-codegen/src/auth/source_builder.ts +++ b/packages/amplify-gen2-codegen/src/auth/source_builder.ts @@ -58,6 +58,10 @@ export type EmailOptions = { emailVerificationSubject: string; }; +export type PhoneOptions = { + verificationMessage: string; +}; + export type StandardAttributes = Partial>; export type CustomAttributes = Partial>; @@ -90,7 +94,7 @@ export type OidcOptions = { export type LoginOptions = { email?: boolean; - phone?: boolean; + phone?: boolean | Partial; emailOptions?: Partial; googleLogin?: boolean; amazonLogin?: boolean; @@ -105,7 +109,16 @@ export type LoginOptions = { callbackURLs?: string[]; logoutURLs?: string[]; scopes?: Scope[]; - [key: string]: boolean | Partial | string[] | Scope[] | OidcOptions[] | SamlOptions | AttributeMappingRule | undefined; + [key: string]: + | boolean + | Partial + | Partial + | string[] + | Scope[] + | OidcOptions[] + | SamlOptions + | AttributeMappingRule + | undefined; }; export type MultifactorOptions = { @@ -361,9 +374,8 @@ function createExternalProvidersPropertyAssignment( function createLogInWithPropertyAssignment(logInDefinition: LoginOptions = {}, secretErrors: ts.Node[]) { const logInWith = factory.createIdentifier('loginWith'); const assignments: ts.ObjectLiteralElementLike[] = []; - if (logInDefinition.email === true) { - assignments.push(factory.createPropertyAssignment(factory.createIdentifier('email'), factory.createTrue())); - } else if (typeof logInDefinition.emailOptions === 'object') { + + if (typeof logInDefinition.emailOptions === 'object') { const emailDefinitionAssignments: ts.ObjectLiteralElementLike[] = []; if (logInDefinition.emailOptions?.emailVerificationSubject) { @@ -391,10 +403,33 @@ function createLogInWithPropertyAssignment(logInDefinition: LoginOptions = {}, s } const emailDefinitionObject = factory.createObjectLiteralExpression(emailDefinitionAssignments, true); assignments.push(factory.createPropertyAssignment(factory.createIdentifier('email'), emailDefinitionObject)); + } else if (logInDefinition.email === true) { + assignments.push(factory.createPropertyAssignment(factory.createIdentifier('email'), factory.createTrue())); } - if (logInDefinition.phone === true) { + + if (typeof logInDefinition.phone === 'object') { + const phoneOptionAssignments: ts.ObjectLiteralElementLike[] = []; + if (logInDefinition.phone?.verificationMessage) { + phoneOptionAssignments.push( + factory.createPropertyAssignment( + 'verificationMessage', + factory.createArrowFunction( + undefined, + undefined, + [], + undefined, + undefined, + factory.createStringLiteral(logInDefinition.phone.verificationMessage), + ), + ), + ); + } + const phoneObject = factory.createObjectLiteralExpression(phoneOptionAssignments, true); + assignments.push(factory.createPropertyAssignment(factory.createIdentifier('phone'), phoneObject)); + } else if (logInDefinition.phone === true) { assignments.push(factory.createPropertyAssignment(factory.createIdentifier('phone'), factory.createTrue())); } + if ( logInDefinition.amazonLogin || logInDefinition.googleLogin ||