title | description | author | manager | ms.author | ms.service | ms.subservice | ms.topic | ms.date |
---|---|---|---|---|---|---|---|---|
Sign in users in a sample React single-page application |
Learn how to configure a sample React single-page app (SPA) to sign in and sign out users. |
garrodonnell |
celestedg |
godonnell |
entra-external-id |
customers |
sample |
04/10/2024 |
This guide uses a sample React single-page application (SPA) to demonstrate how to add authentication to a SPA. This SPA enables users to sign in and sign out by using your external tenant. The sample uses the Microsoft Authentication Library for JavaScript (MSAL.js) to handle authentication.
- Visual Studio Code or another code editor.
- Node.js.
- An external tenant. If you don't already have one, sign up for a free trial.
[!INCLUDE active-directory-b2c-register-app] [!INCLUDE active-directory-b2c-app-integration-add-user-flow]
[!INCLUDE active-directory-b2c-grant-delegated-permissions]
[!INCLUDE active-directory-b2c-app-integration-add-user-flow]
[!INCLUDE active-directory-b2c-app-integration-add-user-flow]
To obtain the sample application, you can either clone it from GitHub or download it as a .zip file.
-
To clone the sample, open a command prompt and navigate to where you wish to create the project, and enter the following command:
git clone https://github.com/Azure-Samples/ms-identity-ciam-javascript-tutorial.git
-
Download the sample. Extract it to a file path where the length of the name is fewer than 260 characters.
-
Open a terminal window in the root directory of the sample project, and enter the following snippet to navigate to the project folder:
cd 1-Authentication\1-sign-in-react\SPA
-
Install the project dependencies:
npm install
-
Open SPA\src\authConfig.js and replace the following with the values obtained from the Microsoft Entra admin center:
Enter_the_Application_Id_Here
and replace it with the Application (client) ID of the app you registered earlier.Enter_the_Tenant_Subdomain_Here
and replace it with the Directory (tenant) subdomain. For example, if your tenant primary domain iscontoso.onmicrosoft.com
, usecontoso
. If you don't have your tenant name, learn how to read your tenant details.
-
Save the file.
All the required code snippets have been added, so the application can now be called and tested in a web browser.
-
Open a new terminal by selecting Terminal > New Terminal.
-
Run the following command to start your web server.
cd 1-Authentication\1-sign-in-react\SPA npm start
-
Open a web browser and navigate to
http://localhost:3000/
. -
Sign-in with an account registered to the external tenant.
-
Once signed in the display name is shown next to the Sign out button.