Skip to content

Commit 130c752

Browse files
committed
chore: adding CDK example for enabling email mfa
1 parent ff83090 commit 130c752

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed

cspell.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,7 @@
851851
"metadata",
852852
"mfaDescription",
853853
"mfaTypes",
854+
"enabledMfas",
854855
"MiB",
855856
"middleware",
856857
"Millis",

src/pages/[platform]/build-a-backend/auth/concepts/multi-factor-authentication/index.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ export const auth = defineAuth({
5656

5757
<Callout info>
5858
Email-based MFA is currently not supported with `defineAuth`. We are working towards supporting this feature. For more information, visit the [feature request in GitHub](https://github.com/aws-amplify/amplify-backend/issues/2159).
59+
60+
To take advantage of this feature with an Amplify generated backend, the underlying CDK construct can be extended manually. See [overriding Cognito User Pool multi-factor authentication options](/[platform]/build-a-backend/auth/modify-resources-with-cdk/#override-cognito-userpool-multi-factor-authentication-options) for more information.
5961
</Callout>
6062

6163
When MFA is `REQUIRED` with SMS in your backend auth resource, you will need to pass the phone number during sign-up API call. If you are using the `email` or `username` as the primary sign-in mechanism, you will need to pass the `phone_number` attribute as a user attribute.

src/pages/[platform]/build-a-backend/auth/modify-resources-with-cdk/index.mdx

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,61 @@ cfnUserPool.policies = {
5757
};
5858
```
5959

60+
## Override Cognito UserPool multi-factor authentication options
61+
While Email MFA is not yet supported with `defineAuth`, this feature can be enabled by modifying the underlying CDK construct.
62+
63+
Start by ensuring your `defineAuth` resource configuration includes a compatible account recovery option and a custom SES sender.
64+
65+
```ts title="amplify/auth/resource.ts"
66+
import { defineAuth } from "@aws-amplify/backend"
67+
68+
/**
69+
* Define and configure your auth resource
70+
* @see https://docs.amplify.aws/gen2/build-a-backend/auth
71+
*/
72+
export const auth = defineAuth({
73+
loginWith: {
74+
email: true,
75+
phone: true,
76+
},
77+
multifactor: {
78+
mode: "OPTIONAL",
79+
sms: true,
80+
totp: false,
81+
},
82+
// Important! The logic to resolve this value cannot determine whether email mfa is enabled when overriding the resource.
83+
// Be sure to pick a recovery option appropriate for your application.
84+
accountRecovery: "EMAIL_AND_PHONE_WITHOUT_MFA",
85+
senders: {
86+
email: {
87+
fromEmail: "[email protected]",
88+
},
89+
},
90+
})
91+
```
92+
Next, extend the underlying CDK construct by activating Advanced Security Mode and adding `EMAIL_OTP` to the enabled MFA options.
93+
94+
```ts title="amplify/backend.ts"
95+
96+
import { defineBackend } from "@aws-amplify/backend"
97+
import { auth } from "./auth/resource"
98+
99+
const backend = defineBackend({
100+
auth,
101+
})
102+
103+
const { cfnUserPool } = backend.auth.resources.cfnResources
104+
105+
// enable ASF
106+
cfnUserPool.userPoolAddOns = {
107+
advancedSecurityMode: "AUDIT",
108+
}
109+
110+
// add email mfa
111+
// https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-cognito-userpool.html#cfn-cognito-userpool-enabledmfas
112+
cfnUserPool.enabledMfas = [...(cfnUserPool.enabledMfas || []), "EMAIL_OTP"]
113+
```
114+
60115
{/* token validity */}
61116
{/* BYO custom idp construct */}
62117
{/* extend auth/unauth roles */}

0 commit comments

Comments
 (0)