@@ -57,6 +57,12 @@ void main() {
5757
5858 await signOutUser (assertComplete: true );
5959
60+ // Verify we can sign in with EMAIL MFA as the preferred method and forego selection.
61+
62+ final otpResult2 = await getOtpCode (
63+ env.getLoginAttribute (username),
64+ );
65+
6066 final resignInRes = await Amplify .Auth .signIn (
6167 username: username,
6268 password: password,
@@ -68,10 +74,6 @@ void main() {
6874 .has ((d) => d.deliveryMedium, 'deliveryMedium' )
6975 .equals (DeliveryMedium .email);
7076
71- final otpResult2 = await getOtpCode (
72- env.getLoginAttribute (username),
73- );
74-
7577 final confirmRes = await Amplify .Auth .confirmSignIn (
7678 confirmationValue: await otpResult2.code,
7779 );
@@ -83,6 +85,8 @@ void main() {
8385 final password = generatePassword ();
8486 final phoneNumber = generatePhoneNumber ();
8587
88+ final mfaCode = await getOtpCode (UserAttribute .phone (phoneNumber));
89+
8690 // Verify we can set EMAIL as preferred and forego selection.
8791 await adminCreateUser (
8892 username,
@@ -95,22 +99,19 @@ void main() {
9599 },
96100 );
97101
98- {
99- final mfaCode = await getOtpCode (UserAttribute .phone (phoneNumber));
100- final signInRes = await Amplify .Auth .signIn (
101- username: username,
102- password: password,
103- );
104- check (
105- signInRes.nextStep.signInStep,
106- because: 'MFA is required so Cognito automatically enables SMS MFA' ,
107- ).equals (AuthSignInStep .confirmSignInWithSmsMfaCode);
102+ final signInRes = await Amplify .Auth .signIn (
103+ username: username,
104+ password: password,
105+ );
106+ check (
107+ signInRes.nextStep.signInStep,
108+ because: 'MFA is required so Cognito automatically enables SMS MFA' ,
109+ ).equals (AuthSignInStep .confirmSignInWithSmsMfaCode);
108110
109- final confirmRes = await Amplify .Auth .confirmSignIn (
110- confirmationValue: await mfaCode.code,
111- );
112- check (confirmRes.nextStep.signInStep).equals (AuthSignInStep .done);
113- }
111+ final confirmRes = await Amplify .Auth .confirmSignIn (
112+ confirmationValue: await mfaCode.code,
113+ );
114+ check (confirmRes.nextStep.signInStep).equals (AuthSignInStep .done);
114115
115116 check (
116117 await cognitoPlugin.fetchMfaPreference (),
@@ -136,6 +137,9 @@ void main() {
136137 await signOutUser (assertComplete: true );
137138
138139 {
140+ final otpResult = await getOtpCode (
141+ env.getLoginAttribute (username),
142+ );
139143 final resignInRes = await Amplify .Auth .signIn (
140144 username: username,
141145 password: password,
@@ -149,10 +153,6 @@ void main() {
149153 .has ((d) => d.deliveryMedium, 'deliveryMedium' )
150154 .equals (DeliveryMedium .email);
151155
152- final otpResult = await getOtpCode (
153- env.getLoginAttribute (username),
154- );
155-
156156 final confirmRes = await Amplify .Auth .confirmSignIn (
157157 confirmationValue: await otpResult.code,
158158 );
@@ -166,43 +166,6 @@ void main() {
166166 ),
167167 );
168168
169- // Verify we can switch to SMS as preferred.
170-
171- await cognitoPlugin.updateMfaPreference (
172- sms: MfaPreference .preferred,
173- );
174- check (await cognitoPlugin.fetchMfaPreference ()).equals (
175- const UserMfaPreference (
176- enabled: {MfaType .sms, MfaType .email},
177- preferred: MfaType .sms,
178- ),
179- );
180-
181- {
182- await signOutUser (assertComplete: true );
183-
184- final mfaCode = await getOtpCode (UserAttribute .phone (phoneNumber));
185- final signInRes = await Amplify .Auth .signIn (
186- username: username,
187- password: password,
188- );
189- check (
190- signInRes.nextStep.signInStep,
191- because: 'Preference is SMS MFA now' ,
192- ).equals (AuthSignInStep .confirmSignInWithSmsMfaCode);
193- check (signInRes.nextStep.codeDeliveryDetails).isNotNull ()
194- ..has ((d) => d.deliveryMedium, 'deliveryMedium' )
195- .equals (DeliveryMedium .sms)
196- ..has ((d) => d.destination, 'destination' )
197- .isNotNull ()
198- .startsWith ('+' );
199-
200- final confirmRes = await Amplify .Auth .confirmSignIn (
201- confirmationValue: await mfaCode.code,
202- );
203- check (confirmRes.nextStep.signInStep).equals (AuthSignInStep .done);
204- }
205-
206169 // Verify marking enabled does not change preference.
207170 await cognitoPlugin.updateMfaPreference (
208171 sms: MfaPreference .enabled,
@@ -214,7 +177,7 @@ void main() {
214177 ).equals (
215178 const UserMfaPreference (
216179 enabled: {MfaType .sms, MfaType .email},
217- preferred: MfaType .sms ,
180+ preferred: MfaType .email ,
218181 ),
219182 );
220183
@@ -246,6 +209,8 @@ void main() {
246209 final password = generatePassword ();
247210 final phoneNumber = generatePhoneNumber ();
248211
212+ final mfaCode = await getOtpCode (UserAttribute .phone (phoneNumber));
213+
249214 // Create a user with an unverified phone number.
250215 await adminCreateUser (
251216 username,
@@ -258,22 +223,19 @@ void main() {
258223 },
259224 );
260225
261- {
262- final mfaCode = await getOtpCode (UserAttribute .phone (phoneNumber));
263- final signInRes = await Amplify .Auth .signIn (
264- username: username,
265- password: password,
266- );
267- check (
268- signInRes.nextStep.signInStep,
269- because: 'MFA is required so Cognito automatically enables SMS MFA' ,
270- ).equals (AuthSignInStep .confirmSignInWithSmsMfaCode);
226+ final signInRes = await Amplify .Auth .signIn (
227+ username: username,
228+ password: password,
229+ );
230+ check (
231+ signInRes.nextStep.signInStep,
232+ because: 'MFA is required so Cognito automatically enables SMS MFA' ,
233+ ).equals (AuthSignInStep .confirmSignInWithSmsMfaCode);
271234
272- final confirmRes = await Amplify .Auth .confirmSignIn (
273- confirmationValue: await mfaCode.code,
274- );
275- check (confirmRes.nextStep.signInStep).equals (AuthSignInStep .done);
276- }
235+ final confirmRes = await Amplify .Auth .confirmSignIn (
236+ confirmationValue: await mfaCode.code,
237+ );
238+ check (confirmRes.nextStep.signInStep).equals (AuthSignInStep .done);
277239
278240 check (
279241 await cognitoPlugin.fetchMfaPreference (),
@@ -287,17 +249,6 @@ void main() {
287249
288250 // Verify we can set SMS as preferred and forego selection.
289251
290- await cognitoPlugin.updateMfaPreference (
291- sms: MfaPreference .preferred,
292- email: MfaPreference .enabled,
293- );
294- check (await cognitoPlugin.fetchMfaPreference ()).equals (
295- const UserMfaPreference (
296- enabled: {MfaType .sms, MfaType .email},
297- preferred: MfaType .sms,
298- ),
299- );
300-
301252 {
302253 await signOutUser (assertComplete: true );
303254
@@ -338,6 +289,10 @@ void main() {
338289 {
339290 await signOutUser (assertComplete: true );
340291
292+ final otpResult = await getOtpCode (
293+ env.getLoginAttribute (username),
294+ );
295+
341296 final signInRes = await Amplify .Auth .signIn (
342297 username: username,
343298 password: password,
@@ -351,10 +306,6 @@ void main() {
351306 .has ((d) => d.deliveryMedium, 'deliveryMedium' )
352307 .equals (DeliveryMedium .email);
353308
354- final otpResult = await getOtpCode (
355- env.getLoginAttribute (username),
356- );
357-
358309 final confirmRes = await Amplify .Auth .confirmSignIn (
359310 confirmationValue: await otpResult.code,
360311 );
0 commit comments