Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: auth codegen to include phone verification setting #13979

Draft
wants to merge 1 commit into
base: migrations
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
fix: auth codegen to include phone verification setting
Hoang Nguyen committed Oct 17, 2024
commit 26c68929311cd7962a5a1a9802c2c9d320007a24
Original file line number Diff line number Diff line change
@@ -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 {####}';
Original file line number Diff line number Diff line change
@@ -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;
}
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
@@ -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: {
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
@@ -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>>;

@@ -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;
@@ -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 = {
@@ -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 ||