Skip to content

Commit 7358405

Browse files
committed
MFA auth
1 parent 9422213 commit 7358405

File tree

1 file changed

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

1 file changed

+65
-1
lines changed

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

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,71 @@ This will create a user with the username and password you provided and add them
126126

127127
The `getSecret` function will fetch the secret you have created using `npx ampx sandbox secret set`. This is useful when you have a public repository and don't want to commit secrets to the repository. Alternatively, you can set the username and password directly as a `string` in the `createAndSignUpUser` function but we recommend using secrets to avoid exposing sensitive information.
128128

129-
{/* MFA example */}
129+
### Auth with TOTP MFA
130+
131+
For example, if you would like to seed your auth with a user with TOTP MFA enabled, lets start by configuring the auth resource:
132+
133+
```typescript title="amplify/auth/resource.ts"
134+
import { defineAuth } from "@aws-amplify/backend";
135+
136+
export const auth = defineAuth({
137+
loginWith: {
138+
email: true,
139+
},
140+
141+
multifactor: {
142+
mode: "REQUIRED",
143+
totp: true,
144+
},
145+
});
146+
```
147+
148+
Now to create a user with TOTP MFA enabled, you can write the following script:
149+
For this example, we will use the `otpauth` library to generate TOTP codes.
150+
151+
```typescript title="amplify/seed/seed.ts"
152+
import {
153+
ChallengeResponse,
154+
createAndSignUpUser,
155+
getSecret,
156+
} from "@aws-amplify/seed";
157+
import { Amplify } from "aws-amplify";
158+
import * as auth from "aws-amplify/auth";
159+
import * as otpauth from "otpauth";
160+
import outputs from "../../amplify_outputs.json";
161+
Amplify.configure(outputs);
162+
const username = await getSecret("username1");
163+
const password = await getSecret("password1");
164+
const setUpTOTPAndChallenge = async (
165+
totpSetup: auth.SetUpTOTPOutput
166+
): Promise<ChallengeResponse> => {
167+
// Using otpauth library to generate TOTP codes
168+
const totp = new otpauth.TOTP({ secret: totpSetup.sharedSecret });
169+
const answer = totp.generate();
170+
return { challengeResponse: answer };
171+
};
172+
const user = await createAndSignUpUser({
173+
username: username,
174+
password: password,
175+
signInAfterCreation: true,
176+
signInFlow: "MFA",
177+
mfaPreference: "TOTP",
178+
totpSignUpChallenge: async (totpSetup) => {
179+
return await setUpTOTPAndChallenge(totpSetup);
180+
},
181+
});
182+
183+
console.log(`User ${user.username} was created`);
184+
auth.signOut();
185+
```
186+
187+
This will create a user with the username and password with TOTP MFA enabled. The TOTP code is generated using the `otpauth` library.
188+
189+
Run the seed script
190+
191+
```bash title="Terminal" showLineNumbers={false}
192+
npx ampx sandbox seed
193+
```
130194

131195
### Data
132196

0 commit comments

Comments
 (0)