Skip to content

Commit 7a57277

Browse files
jjarvispjosefaidt
andauthored
docs(js): more passwordless sign in examples (aws-amplify#8135)
* more sign in examples * Apply suggestions from code review Co-authored-by: josef <[email protected]> * fix typo --------- Co-authored-by: josef <[email protected]>
1 parent 943d681 commit 7a57277

File tree

1 file changed

+82
-29
lines changed
  • src/pages/[platform]/build-a-backend/auth/connect-your-frontend/sign-in

1 file changed

+82
-29
lines changed

src/pages/[platform]/build-a-backend/auth/connect-your-frontend/sign-in/index.mdx

+82-29
Original file line numberDiff line numberDiff line change
@@ -1120,17 +1120,28 @@ Your application's users can also sign in using passwordless methods. To learn m
11201120

11211121
<InlineFilter filters={["angular", "javascript", "nextjs", "react", "react-native", "vue"]}>
11221122

1123-
To request an OTP code via SMS for authentication, the challenge is passed as the challenge response to the confirm sign in API.
1123+
Pass `SMS_OTP` as the `preferredChallenge` when calling the `signIn` API in order to initiate a passwordless authentication flow with SMS OTP.
11241124

1125-
Amplify will respond appropriately to Cognito and return the challenge as sign in next step: `CONFIRM_SIGN_IN_WITH_SMS_CODE`:
11261125

11271126
```ts
1128-
const { nextStep } = await confirmSignIn({
1129-
challengeResponse: "SMS_OTP"
1127+
const { nextStep: signInNextStep } = await signIn({
1128+
username: '+15551234567',
1129+
options: {
1130+
authFlowType: 'USER_AUTH',
1131+
preferredChallenge: 'SMS_OTP',
1132+
},
11301133
});
11311134

1132-
// nextStep.signInStep === 'CONFIRM_SIGN_IN_WITH_SMS_CODE'
1133-
handleNextSignInStep(nextStep);
1135+
if (signInNextStep.signInStep === 'CONFIRM_SIGN_IN_WITH_SMS_CODE') {
1136+
// prompt user for otp code delivered via SMS
1137+
const { nextStep: confirmSignInNextStep } = await confirmSignIn({
1138+
challengeResponse: '123456',
1139+
});
1140+
1141+
if (confirmSignInNextStep.signInStep === 'DONE') {
1142+
console.log('Sign in successful!');
1143+
}
1144+
}
11341145
```
11351146

11361147
</InlineFilter>
@@ -1223,17 +1234,27 @@ func confirmSignIn() -> AnyCancellable {
12231234

12241235
<InlineFilter filters={["angular", "javascript", "nextjs", "react", "react-native", "vue"]}>
12251236

1226-
To request an OTP code via email for authentication, the challenge is passed as the challenge response to the confirm sign in API.
1227-
1228-
Amplify will respond appropriately to Cognito and return the challenge as sign in next step: `CONFIRM_SIGN_IN_WITH_EMAIL_CODE`:
1237+
Pass `EMAIL_OTP` as the `preferredChallenge` when calling the `signIn` API in order to initiate a passwordless authentication flow using email OTP.
12291238

12301239
```ts
1231-
const { nextStep } = await confirmSignIn({
1232-
challengeResponse: "EMAIL_OTP"
1240+
const { nextStep: signInNextStep } = await signIn({
1241+
username: '[email protected]',
1242+
options: {
1243+
authFlowType: 'USER_AUTH',
1244+
preferredChallenge: 'EMAIL_OTP',
1245+
},
12331246
});
12341247

1235-
// nextStep.signInStep === 'CONFIRM_SIGN_IN_WITH_EMAIL_CODE'
1236-
handleNextSignInStep(nextStep);
1248+
if (signInNextStep.signInStep === 'CONFIRM_SIGN_IN_WITH_EMAIL_CODE') {
1249+
// prompt user for otp code delivered via email
1250+
const { nextStep: confirmSignInNextStep } = await confirmSignIn({
1251+
challengeResponse: '123456',
1252+
});
1253+
1254+
if (confirmSignInNextStep.signInStep === 'DONE') {
1255+
console.log('Sign in successful!');
1256+
}
1257+
}
12371258
```
12381259

12391260
</InlineFilter>
@@ -1326,15 +1347,20 @@ func confirmSignIn() -> AnyCancellable {
13261347

13271348
<InlineFilter filters={["angular", "javascript", "nextjs", "react", "react-native", "vue"]}>
13281349

1329-
The WebAuthn credential flow is initiated by passing the challenge name to the confirm sign in api.
1350+
Pass `WEB_AUTHN` as the `preferredChallenge` in order to initiate the passwordless authentication flow using a WebAuthn credential.
13301351

13311352
```ts
1332-
const { nextStep } = await confirmSignIn({
1333-
challengeResponse: "WEB_AUTHN",
1353+
const { nextStep: signInNextStep } = await signIn({
1354+
username: '[email protected]',
1355+
options: {
1356+
authFlowType: 'USER_AUTH',
1357+
preferredChallenge: 'WEB_AUTHN',
1358+
},
13341359
});
13351360

1336-
// nextStep.signInStep === 'DONE'
1337-
handleNextSignInStep(nextStep);
1361+
if (signInNextStep.signInStep === 'DONE') {
1362+
console.log('Sign in successful!');
1363+
}
13381364
```
13391365

13401366
</InlineFilter>
@@ -1356,25 +1382,52 @@ handleNextSignInStep(nextStep);
13561382

13571383
<InlineFilter filters={["angular", "javascript", "nextjs", "react", "react-native", "vue"]}>
13581384

1359-
### Password or SRP
1385+
### Password
13601386

1361-
Traditional password based authentication is available from this flow as well. To initiate this flow from select challenge, either `PASSWORD` or `PASSWORD_SRP` is passed as the challenge response.
1387+
Pass either `PASSWORD` or `PASSWORD_SRP` as the `preferredChallenge` in order to initiate a traditional password based authentication flow.
13621388

13631389
```ts
1364-
const { nextStep } = await confirmSignIn({
1365-
challengeResponse: "PASSWORD_SRP", // or "PASSWORD"
1390+
const { nextStep: signInNextStep } = await signIn({
1391+
username: '[email protected]',
1392+
password: 'example-password',
1393+
options: {
1394+
authFlowType: 'USER_AUTH',
1395+
preferredChallenge: 'PASSWORD_SRP', // or 'PASSWORD'
1396+
},
13661397
});
13671398

1368-
// in both cases
1369-
// nextStep.signInStep === 'CONFIRM_SIGN_IN_WITH_PASSWORD'
1370-
handleNextSignInStep(nextStep);
1399+
if (confirmSignInNextStep.signInStep === 'DONE') {
1400+
console.log('Sign in successful!');
1401+
}
1402+
```
1403+
13711404

1372-
const { nextStep: nextNextStep } = await confirmSignIn({
1373-
challengeResponse: "Test123#",
1405+
### First Factor Selection
1406+
1407+
Omit the `preferredChallenge` parameter to discover what first factors are available for a given user.
1408+
1409+
The `confirmSignIn` API can then be used to select a challenge and initiate the associated authentication flow.
1410+
1411+
```ts
1412+
const { nextStep: signInNextStep } = await signIn({
1413+
username: '+15551234567',
1414+
options: {
1415+
authFlowType: 'USER_AUTH',
1416+
},
13741417
});
13751418

1376-
// nextNextStep.signInStep === 'DONE'
1377-
handleNextSignInStep(nextNextStep);
1419+
if (
1420+
signInNextStep.signInStep === 'CONTINUE_SIGN_IN_WITH_FIRST_FACTOR_SELECTION'
1421+
) {
1422+
// present user with list of available challenges
1423+
console.log(`Available Challenges: ${signInNextStep.availableChallenges}`);
1424+
1425+
// respond with user selection using `confirmSignIn` API
1426+
const { nextStep: nextConfirmSignInStep } = await confirmSignIn({
1427+
challengeResponse: 'SMS_OTP', // or 'EMAIL_OTP', 'WEB_AUTHN', 'PASSWORD', 'PASSWORD_SRP'
1428+
});
1429+
}
1430+
13781431
```
13791432

13801433
</InlineFilter>

0 commit comments

Comments
 (0)