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

feat: support configuring SES for email sender #1851

Merged
merged 15 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions .changeset/weak-rats-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aws-amplify/auth-construct': minor
---

add support for configuring SES for email sender
4 changes: 4 additions & 0 deletions packages/auth-construct/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { SecretValue } from 'aws-cdk-lib';
import { StandardAttributes } from 'aws-cdk-lib/aws-cognito';
import { StringAttributeConstraints } from 'aws-cdk-lib/aws-cognito';
import { UserPoolIdentityProviderSamlMetadata } from 'aws-cdk-lib/aws-cognito';
import { UserPoolSESOptions } from 'aws-cdk-lib/aws-cognito';

// @public
export type AmazonProviderProps = Omit<aws_cognito.UserPoolIdentityProviderAmazonProps, 'userPool' | 'attributeMapping'> & IdentityProviderProps;
Expand Down Expand Up @@ -45,6 +46,9 @@ export type AuthProps = {
phone?: PhoneNumberLogin;
externalProviders?: ExternalProviderOptions;
};
senders?: {
email: Pick<UserPoolSESOptions, 'fromEmail' | 'fromName' | 'replyTo'>;
};
userAttributes?: UserAttributes;
multifactor?: MFA;
accountRecovery?: keyof typeof aws_cognito.AccountRecovery;
Expand Down
28 changes: 28 additions & 0 deletions packages/auth-construct/src/construct.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,34 @@ void describe('Auth construct', () => {
);
});

void it('configures Cognito to send emails with SES when senders field is populated', () => {
const app = new App();
const stack = new Stack(app);
const expectedNameAndEmail = 'Example.com <[email protected]>';
const expectedReply = '[email protected]';
const sesEmailSettings = {
fromEmail: '[email protected]',
fromName: 'Example.com',
replyTo: '[email protected]',
};
new AmplifyAuth(stack, 'test', {
loginWith: {
email: true,
},
senders: {
email: sesEmailSettings,
},
});

const template = Template.fromStack(stack);
template.allResourcesProperties('AWS::Cognito::UserPool', {
EmailConfiguration: {
From: expectedNameAndEmail,
ReplyToEmailAddress: expectedReply,
ShadowCat567 marked this conversation as resolved.
Show resolved Hide resolved
},
});
});

void it('requires email attribute if email is enabled', () => {
const app = new App();
const stack = new Stack(app);
Expand Down
8 changes: 8 additions & 0 deletions packages/auth-construct/src/construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,14 @@ export class AmplifyAuth
customAttributes: {
...customAttributes,
},
email: props.senders
? cognito.UserPoolEmail.withSES({
fromEmail: props.senders.email.fromEmail,
fromName: props.senders.email.fromName,
replyTo: props.senders.email.replyTo,
sesRegion: Stack.of(this).region,
})
: undefined,

selfSignUpEnabled: DEFAULTS.ALLOW_SELF_SIGN_UP,
mfa: mfaMode,
Expand Down
13 changes: 13 additions & 0 deletions packages/auth-construct/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
StandardAttributes,
StringAttributeConstraints,
UserPoolIdentityProviderSamlMetadata,
UserPoolSESOptions,
} from 'aws-cdk-lib/aws-cognito';
export type VerificationEmailWithLink = {
/**
Expand Down Expand Up @@ -410,6 +411,18 @@ export type AuthProps = {
*/
externalProviders?: ExternalProviderOptions;
};
/**
* Configure sending behaviors for Emails or SMS messages sent from your auth resource
* @see https://docs.amplify.aws/react/build-a-backend/auth/customize-auth-lifecycle/email-customization/#custom-senders
*/
senders?: {
/**
* Configure Cognito to send emails from SES
* SES configurations enable the use of customized email sender addresses and names
* @see https://docs.amplify.aws/react/build-a-backend/auth/moving-to-production/#email
*/
email: Pick<UserPoolSESOptions, 'fromEmail' | 'fromName' | 'replyTo'>;
};
/**
* The set of attributes that are required for every user in the user pool. Read more on attributes here - https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-attributes.html
* @default - email/phone will be added as required user attributes if they are included as login methods
Expand Down