Skip to content

Commit baed20b

Browse files
committed
address comments
1 parent 5f70ece commit baed20b

File tree

1 file changed

+76
-17
lines changed
  • src/pages/[platform]/deploy-and-host/sandbox-environments/seed

1 file changed

+76
-17
lines changed

src/pages/[platform]/deploy-and-host/sandbox-environments/seed/index.mdx

Lines changed: 76 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ const user = await createAndSignUpUser({
218218

219219
console.log(`User ${user.username} was created`);
220220

221-
// Wait for a moment to ensure we get a fresh TOTP code
221+
// Wait for a moment to get a fresh TOTP code
222222
await new Promise((resolve) => setTimeout(resolve, 35000));
223223

224224
const signIn = await signInUser({
@@ -245,6 +245,66 @@ Run the seed script
245245
npx ampx sandbox seed
246246
```
247247

248+
### Auth with Email MFA
249+
250+
For example, if you would like to seed your auth with a user with Email MFA enabled:
251+
252+
```typescript title="amplify/seed/seed.ts"
253+
import {
254+
createAndSignUpUser,
255+
getSecret,
256+
signInUser
257+
} from "@aws-amplify/seed";
258+
import { Amplify } from "aws-amplify";
259+
import * as auth from "aws-amplify/auth";
260+
import { readFile } from 'node:fs/promises';
261+
262+
// this is used to get the amplify_outputs.json file as the file will not exist until sandbox is created
263+
const url = new URL("../../amplify_outputs.json", import.meta.url);
264+
const outputs = JSON.parse(await readFile(url, { encoding: "utf8" }));
265+
Amplify.configure(outputs);
266+
267+
const username = await getSecret("username");
268+
const password = await getSecret("password");
269+
270+
// Set mfaPreference to EMAIL when using email-only MFA
271+
const user = await createAndSignUpUser({
272+
username: username,
273+
password: password,
274+
signInAfterCreation: false,
275+
signInFlow: "MFA",
276+
mfaPreference: "EMAIL",
277+
});
278+
279+
// Sign in will prompt for MFA code in command line
280+
await signInUser({
281+
username: username,
282+
password: password,
283+
signInFlow: "MFA",
284+
});
285+
286+
auth.signOut();
287+
```
288+
289+
This will create a user with the username and password with Email MFA enabled. The user will then be signed in and prompted for the MFA code in the command line.
290+
291+
```bash title="Terminal" showLineNumbers={false}
292+
npx ampx sandbox seed
293+
seed is running...
294+
✔ Please input one-time password from EMAIL for [email protected]:
295+
User [email protected] was created
296+
✔ Please input one-time password from EMAIL for [email protected]:
297+
User was signed in: true
298+
```
299+
300+
SMS MFA follows the same pattern as Email MFA, using command line prompts for verification. Just replace `mfaPreference: "EMAIL"` with `mfaPreference: "SMS"` in your configuration. The command line experience will be identical, prompting for the SMS code instead of the email code.
301+
302+
<Callout info>
303+
**Note:** 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).
304+
305+
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.
306+
</Callout>
307+
248308
### Data
249309

250310
For example, if you like to seed your Data API, lets start by creating a GraphQL API with a `Todo` model with authorization mode set to `userPool`:
@@ -459,14 +519,16 @@ This behavior is particularly important when seeding multiple users in your appl
459519

460520
### MFA Challenge Handling
461521

462-
- For sign-up challenges, each MFA type has its specific challenge callback:
522+
- For sign-up challenges, each MFA type has its own specific challenge callback:
463523
- TOTP: `totpSignUpChallenge`
464524
- Email: `emailSignUpChallenge`
465-
466-
- For sign-in, there's a single `signInChallenge` callback that works for all MFA types
525+
- SMS: `smsSignUpChallenge`
526+
527+
- For sign-in, there's a single universal `signInChallenge` callback that works with all MFA types (TOTP, Email, or SMS)
467528

468-
- Command line prompts work with all forms of MFA during sign-in
469-
- For sign-up, command line prompts work with EMAIL and SMS, but not with TOTP
529+
Important behaviors:
530+
- Command line prompts work with all MFA types during sign-in
531+
- During sign-up, command line will prompt for EMAIL and SMS MFA, but not for TOTP MFA
470532
- When MFA is set to "Optional" in a user pool, users will be sent through the Password flow
471533

472534
### TOTP Considerations
@@ -530,20 +592,17 @@ Auth APIs allow you to create and manage users in your sandbox environment and a
530592
}, 'GroupName');
531593
```
532594

533-
### Additional APIs
595+
### Additional Types
534596

535-
The `@aws-amplify/seed` package additionally provides the following APIs:
597+
The `@aws-amplify/seed` package provides these essential types:
536598

537-
- `AuthSignUp` - API for user sign-up configuration
538-
- `AuthUser` - API for user authentication information
539-
- `ChallengeResponse` - API for MFA challenge responses
540-
- `StandardUserAttributes` - API for managing user attributes during sign-up
541-
- `PasswordSignInFlow` - API for password-based authentication
542-
- `MfaSignUpFlow` - API for MFA during sign-up
543-
- `MfaSignInFlow` - API for MFA during sign-in
544-
- `MfaWithTotpSignUpFlow` - API for TOTP-specific MFA during sign-up
599+
- `AuthSignUp` - Type for user sign-up configuration
600+
- `AuthUser` - Type for user authentication information
601+
- `ChallengeResponse` - Type for MFA challenge responses
602+
- `StandardUserAttributes` - Type for managing user attributes during sign-up
603+
- `AuthOutputs` - Type for user sign-up output
545604

546-
The following challenge callback APIs are available for MFA flows:
605+
MFA challenge callback types:
547606
- `emailSignUpChallenge` - Handles Email MFA during sign-up
548607
- `smsSignUpChallenge` - Handles SMS MFA during sign-up
549608
- `totpSignUpChallenge` - Handles TOTP MFA during sign-up

0 commit comments

Comments
 (0)