Skip to content

Commit

Permalink
feat: support configuring SES for email sender (#1851)
Browse files Browse the repository at this point in the history
* feat: exposed parameters to be able to configure Cognito to send emails with SES

* feat: updated auth construct to feed new properties to UserPool settings

* empty string in fromEmail field if no email provided

* altered comment describing email configuation

* chore: added changeset

* chore: check if senders is undefined

* chore: updated API

* chore: added test for email configuration

* chore: updated variable name for expected value of fromEmail and fromName

* Revert "chore: updated variable name for expected value of fromEmail and fromName"

This reverts commit 794ba4b.

* chore: changed variable name, merged main into branch

* chore: updated docs for configuring SES email sender

---------

Co-authored-by: Vieltojarvi <[email protected]>
  • Loading branch information
ShadowCat567 and Vieltojarvi authored Aug 19, 2024
1 parent d755f74 commit 61d4fb3
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
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,
},
});
});

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

0 comments on commit 61d4fb3

Please sign in to comment.