Tutorial Native Authentication Single Page App React Sdk Sign Up
In brief
1. Create *sign-up/components/InitialForm.tsx* file, then paste the code from [sign-up/components/InitialForm.tsx](https://github.com/Azure-Samples/ms-identity-ciam-native-javascript-samples/blob/main/typescript/native-auth/react-nextjs-sample/src/app/sign-up…
Documentation change
- Create sign-up/components/InitialForm.tsx file, then paste the code from sign-up/components/InitialForm.tsx. This component displays a form that collects user sign-up attributes.
If you want to let users sign up with a username (alias), add a text input to this form to collect the username (alias) value. For example:
```tsx
<input
type="text"
placeholder="Username (alias)"
value={flatUsername}
onChange={(e) => setFlatUsername(e.target.value)}
style={styles.input}
/>
```
- Create a sign-up/components/CodeForm.tsx file, then paste the code from sign-up/components/CodeForm.tsx. This component displays a form that collects a one-time passcode sent to the user. You require this form for either email with password or email with one-time passcode authentication method.
- Create a sign-up/components/CodeForm.tsx file, then paste the code from sign-up/components/CodeForm.tsx. This component displays a form that collects a one-time passcode sent to the user. You require this form for either email with password or email with one-time passcode authentication method.
If your choice of authentication method is email with password, create a sign-up/components/PasswordForm.tsx file, then paste the code from sign-up/components/PasswordForm.tsx. This component displays a password input form.
The SDK's instance method, `signUp()` starts the sign-up flow.
If you want to let users sign up with a username (alias), add a `flatUsername` state to the sign-up page, then include the `flatusername` attribute in the `UserAccountAttributes` you pass to `signUp()`:
```typescript
const [flatUsername, setFlatUsername] = useState("");
const attributes: UserAccountAttributes = {
displayName: `${firstName} ${lastName}`,
givenName: firstName,
surname: lastName,
jobTitle: jobTitle,
city: city,
country: country,
flatusername: flatUsername,
};
```
After sign-up, the user can sign in by using either their email or their username (alias). When you handle errors for the username (alias), keep in mind that:
- `result.error?.isUserAlreadyExists()` covers a duplicate email *or* a duplicate username (alias). Update the message accordingly, for example, *An account with this email or username already exists*.
- An invalid username (alias) is surfaced through `result.error?.isAttributesValidationFailed()` rather than `result.error?.isInvalidUsername()`. Branch on this method to show a username-specific message.
To handle the one-time passcode submission, use the following code snippet. See a full example at sign-up/page.tsx to learn where to place the code snippet:
return <SignUpResultPage/>; }
Collect a username (alias) during sign-up
You can let users sign up with a username (alias) in addition to their email. The username (alias) is an alternate sign-in identifier, such as a customer ID, account number, or another value that you choose.
During sign-up, the username (email) is always required as the primary identifier, and the username (alias) doesn't replace it. By default, the username (alias) is optional, though an administrator can configure it as required. Your app always collects the username (email) and collects the alias as an attribute alongside the email. At sign-in, the user can then sign in with either their username (email) or their username (alias). To learn how the Username attribute is configured as optional or required, see Configure the user input types and page layout.
To collect a username (alias) during sign-up:
Make sure the Username built-in user attribute is enabled in your sign-up user flow. For the steps, see Enable username in the sign-in identifier policy.
Add a
flatUsernamestate to the sign-up page, then include theflatusernameattribute in theUserAccountAttributesyou pass tosignUp():const [flatUsername, setFlatUsername] = useState(""); const attributes: UserAccountAttributes = { displayName: `${firstName} ${lastName}`, givenName: firstName, surname: lastName, jobTitle: jobTitle, city: city, country: country, flatusername: flatUsername, };Add an alias input to InitialForm.tsx to collect the username (alias) value:
<input type="text" placeholder="Username (alias)" value={flatUsername} onChange={(e) => setFlatUsername(e.target.value)} style={styles.input} />Handle errors related to the username (alias):
result.error?.isUserAlreadyExists()covers a duplicate email or a duplicate username (alias). Update the message accordingly, for example, An account with this email or username already exists.- An invalid username (alias) is surfaced through
result.error?.isAttributesValidationFailed()rather thanresult.error?.isInvalidUsername(). Branch on this method to show a username-specific message.
Handle sign-up errors
During sign-up, not all actions succeed. For instance, the user might attempt to sign up with an already used email address or submit an invalid email one-time passcode. Make sure you handle errors properly when you:
diff --git a/docs/identity-platform/tutorial-native-authentication-single-page-app-react-sdk-sign-up.md b/docs/identity-platform/tutorial-native-authentication-single-page-app-react-sdk-sign-up.md
index 6e1a1d297ef..9efdf070abc 100644
--- a/docs/identity-platform/tutorial-native-authentication-single-page-app-react-sdk-sign-up.md
+++ b/docs/identity-platform/tutorial-native-authentication-single-page-app-react-sdk-sign-up.md
@@ -147,19 +147,7 @@ This app collects user details such as given name, username (email), password, a
1. Create *sign-up/components/InitialForm.tsx* file, then paste the code from [sign-up/components/InitialForm.tsx](https://github.com/Azure-Samples/ms-identity-ciam-native-javascript-samples/blob/main/typescript/native-auth/react-nextjs-sample/src/app/sign-up/components/InitialForm.tsx). This component displays a form that collects user sign-up attributes.
- If you want to let users sign up with a username (alias), add a text input to this form to collect the username (alias) value. For example:
-
- ```tsx
- <input
- type="text"
- placeholder="Username (alias)"
- value={flatUsername}
- onChange={(e) => setFlatUsername(e.target.value)}
- style={styles.input}
- />
- ```
-
-1. Create a *sign-up/components/CodeForm.tsx* file, then paste the code from [sign-up/components/CodeForm.tsx](https://github.com/Azure-Samples/ms-identity-ciam-native-javascript-samples/blob/main/typescript/native-auth/react-nextjs-sample/src/app/shared/components/CodeForm.tsx). This component displays a form that collects a one-time passcode sent to the user. You require this form for either email with password or email with one-time passcode authentication method.
+1. Create a *sign-up/components/CodeForm.tsx* file, then paste the code from [sign-up/components/CodeForm.tsx](https://github.com/Azure-Samples/ms-identity-ciam-native-javascript-samples/blob/main/typescript/native-auth/react-nextjs-sample/src/app/shared/components/CodeForm.tsx). This component displays a form that collects a one-time passcode sent to the user. You require this form for either email with password or email with one-time passcode authentication method.
1. If your choice of authentication method is *email with password*, create a *sign-up/components/PasswordForm.tsx* file, then paste the code from [sign-up/components/PasswordForm.tsx](https://github.com/Azure-Samples/ms-identity-ciam-native-javascript-samples/blob/main/typescript/native-auth/react-nextjs-sample/src/app/shared/components/PasswordForm.tsx). This component displays a password input form.
@@ -346,27 +334,6 @@ Create *sign-up/page.tsx* to handle logic for a sign-up flow. In this file:
```
The SDK's instance method, `signUp()` starts the sign-up flow.
- If you want to let users sign up with a username (alias), add a `flatUsername` state to the sign-up page, then include the `flatusername` attribute in the `UserAccountAttributes` you pass to `signUp()`:
-
- ```typescript
- const [flatUsername, setFlatUsername] = useState("");
-
- const attributes: UserAccountAttributes = {
- displayName: `${firstName} ${lastName}`,
- givenName: firstName,
- surname: lastName,
- jobTitle: jobTitle,
- city: city,
- country: country,
- flatusername: flatUsername,
- };
- ```
-
- After sign-up, the user can sign in by using either their email or their username (alias). When you handle errors for the username (alias), keep in mind that:
-
- - `result.error?.isUserAlreadyExists()` covers a duplicate email *or* a duplicate username (alias). Update the message accordingly, for example, *An account with this email or username already exists*.
- - An invalid username (alias) is surfaced through `result.error?.isAttributesValidationFailed()` rather than `result.error?.isInvalidUsername()`. Branch on this method to show a username-specific message.
-
- To handle the one-time passcode submission, use the following code snippet. See a full example at [sign-up/page.tsx](https://github.com/Azure-Samples/ms-identity-ciam-native-javascript-samples/blob/main/typescript/native-auth/react-nextjs-sample/src/app/sign-up/page.tsx) to learn where to place the code snippet:
```typescript
@@ -434,7 +401,50 @@ Create *sign-up/page.tsx* to handle logic for a sign-up flow. In this file:
return <SignUpResultPage/>;
}
```
-
+
+## Collect a username (alias) during sign-up
+
+You can let users sign up with a username (alias) in addition to their email. The username (alias) is an alternate sign-in identifier, such as a customer ID, account number, or another value that you choose.
+
+During sign-up, the username (email) is always required as the primary identifier, and the username (alias) doesn't replace it. By default, the username (alias) is optional, though an administrator can configure it as required. Your app always collects the username (email) and collects the alias as an attribute alongside the email. At sign-in, the user can then sign in with either their username (email) or their username (alias). To learn how the **Username** attribute is configured as optional or required, see [Configure the user input types and page layout](../external-id/customers/how-to-define-custom-attributes.md#configure-the-user-input-types-and-page-layout).
+
+To collect a username (alias) during sign-up:
+
+1. Make sure the **Username** built-in user attribute is enabled in your sign-up user flow. For the steps, see [Enable username in the sign-in identifier policy](../external-id/customers/how-to-sign-in-alias.md#enable-username-in-sign-in-identifier-policy).
+
+1. Add a `flatUsername` state to the sign-up page, then include the `flatusername` attribute in the `UserAccountAttributes` you pass to `signUp()`:
+
+ ```typescript
+ const [flatUsername, setFlatUsername] = useState("");
+
+ const attributes: UserAccountAttributes = {
+ displayName: `${firstName} ${lastName}`,
+ givenName: firstName,
+ surname: lastName,
+ jobTitle: jobTitle,
+ city: city,
+ country: country,
+ flatusername: flatUsername,
+ };
+ ```
+
+1. Add an alias input to *InitialForm.tsx* to collect the username (alias) value:
+
+ ```tsx
+ <input
+ type="text"
+ placeholder="Username (alias)"
+ value={flatUsername}
+ onChange={(e) => setFlatUsername(e.target.value)}
+ style={styles.input}
+ />
+ ```
+
+1. Handle errors related to the username (alias):
+
+ - `result.error?.isUserAlreadyExists()` covers a duplicate email *or* a duplicate username (alias). Update the message accordingly, for example, *An account with this email or username already exists*.
+ - An invalid username (alias) is surfaced through `result.error?.isAttributesValidationFailed()` rather than `result.error?.isInvalidUsername()`. Branch on this method to show a username-specific message.
+
## Handle sign-up errors
During sign-up, not all actions succeed. For instance, the user might attempt to sign up with an already used email address or submit an invalid email one-time passcode. Make sure you handle errors properly when you: