Skip to content

Commit

Permalink
fix: auth codegen to include phone verification setting
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoang Nguyen committed Oct 17, 2024
1 parent aa5cb3c commit 26c6892
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {####}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
13 changes: 13 additions & 0 deletions packages/amplify-gen2-codegen/src/auth/source_builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
47 changes: 41 additions & 6 deletions packages/amplify-gen2-codegen/src/auth/source_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ export type EmailOptions = {
emailVerificationSubject: string;
};

export type PhoneOptions = {
verificationMessage: string;
};

export type StandardAttributes = Partial<Record<Attribute, StandardAttribute>>;
export type CustomAttributes = Partial<Record<`custom:${string}`, CustomAttribute>>;

Expand Down Expand Up @@ -90,7 +94,7 @@ export type OidcOptions = {

export type LoginOptions = {
email?: boolean;
phone?: boolean;
phone?: boolean | Partial<PhoneOptions>;
emailOptions?: Partial<EmailOptions>;
googleLogin?: boolean;
amazonLogin?: boolean;
Expand All @@ -105,7 +109,16 @@ export type LoginOptions = {
callbackURLs?: string[];
logoutURLs?: string[];
scopes?: Scope[];
[key: string]: boolean | Partial<EmailOptions> | string[] | Scope[] | OidcOptions[] | SamlOptions | AttributeMappingRule | undefined;
[key: string]:
| boolean
| Partial<EmailOptions>
| Partial<PhoneOptions>
| string[]
| Scope[]
| OidcOptions[]
| SamlOptions
| AttributeMappingRule
| undefined;
};

export type MultifactorOptions = {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 ||
Expand Down

0 comments on commit 26c6892

Please sign in to comment.