Skip to content

Commit b1200b8

Browse files
feat(js) adds select-mfa-type challenge to MFA section, replaces QRCode with QRCodeCanvas
1 parent 64e5624 commit b1200b8

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/fragments/lib/auth/js/mfa.mdx

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ import { Auth } from 'aws-amplify';
2222
Auth.setupTOTP(user).then((code) => {
2323
// You can directly display the `code` to the user or convert it to a QR code to be scanned.
2424
// E.g., use following code sample to render a QR code with `qrcode.react` component:
25-
// import QRCode from 'qrcode.react';
25+
// import QRCodeCanvas from 'qrcode.react';
2626
// const str = "otpauth://totp/AWSCognito:"+ username + "?secret=" + code + "&issuer=" + issuer;
27-
// <QRCode value={str}/>
27+
// <QRCodeCanvas value={str}/>
2828
});
2929

3030
// ...
@@ -104,6 +104,8 @@ ChallengeName:
104104
- `SOFTWARE_TOKEN_MFA`: The user needs to input the OTP(one time password). You can submit the code by `Auth.confirmSignIn`.
105105
- `NEW_PASSWORD_REQUIRED`: This happens when the user account is created through the Cognito console. The user needs to input the new password and required attributes. You can submit those data by `Auth.completeNewPassword`.
106106
- `MFA_SETUP`: This happens when the MFA method is TOTP(the one time password) which requires the user to go through some steps to generate those passwords. You can start the setup process by `Auth.setupTOTP`.
107+
- `SELECT_MFA_TYPE`: This happens when both SMS and TOTP methods are enabled but neither is set as preferred. You can set up your front-end application to let users choose what type they want to use
108+
in the current session and set it using `user.sendMFASelectionAnswer` function.
107109

108110
The following code is only for demonstration purpose:
109111

@@ -146,6 +148,21 @@ async function signIn() {
146148
// The user needs to setup the TOTP before using it
147149
// More info please check the Enabling MFA part
148150
Auth.setupTOTP(user);
151+
} else if (user.challengeName === 'SELECT_MFA_TYPE') {
152+
// You need to get the MFA method (SMS or TOTP) from user
153+
// and trigger the following function
154+
// user object needs to be CognitoUser type
155+
user.sendMFASelectionAnswer(mfaType, {
156+
onFailure: (err) => {
157+
console.error(err);
158+
},
159+
mfaRequired: (challengeName, parameters) => {
160+
// Auth.confirmSignIn with SMS code
161+
},
162+
totpRequired: (challengeName, parameters) => {
163+
// Auth.confirmSignIn with TOTP code
164+
}
165+
});
149166
} else {
150167
// The user directly signs in
151168
console.log(user);

src/fragments/lib/auth/js/react-native-mfa.mdx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ ChallengeName:
9292
- `SOFTWARE_TOKEN_MFA`: The user needs to input the OTP(one time password). You can submit the code by `Auth.confirmSignIn`.
9393
- `NEW_PASSWORD_REQUIRED`: This happens when the user account is created through the Cognito console. The user needs to input the new password and required attributes. You can submit those data by `Auth.completeNewPassword`.
9494
- `MFA_SETUP`: This happens when the MFA method is TOTP(the one time password) which requires the user to go through some steps to generate those passwords. You can start the setup process by `Auth.setupTOTP`.
95+
- `SELECT_MFA_TYPE`: This happens when both SMS and TOTP methods are enabled but neither is set as preferred. You can set up your front-end application to let users choose what type they want to use
96+
in the current session and set it using `user.sendMFASelectionAnswer` function.
9597

9698
The following code is only for demonstration purpose:
9799

@@ -134,6 +136,21 @@ async function signIn() {
134136
// The user needs to setup the TOTP before using it
135137
// More info please check the Enabling MFA part
136138
Auth.setupTOTP(user);
139+
} else if (user.challengeName === 'SELECT_MFA_TYPE') {
140+
// You need to get the MFA method (SMS or TOTP) from user
141+
// and trigger the following function
142+
// user object needs to be CognitoUser type
143+
user.sendMFASelectionAnswer(mfaType, {
144+
onFailure: (err) => {
145+
console.error(err);
146+
},
147+
mfaRequired: (challengeName, parameters) => {
148+
// Auth.confirmSignIn with SMS code
149+
},
150+
totpRequired: (challengeName, parameters) => {
151+
// Auth.confirmSignIn with TOTP code
152+
}
153+
});
137154
} else {
138155
// The user directly signs in
139156
console.log(user);

0 commit comments

Comments
 (0)