title | description | author | manager | ms.author | ms.service | ms.subservice | ms.custom | ms.topic | ms.date |
---|---|---|---|---|---|---|---|---|---|
Sign in users in a sample vanilla JavaScript single-page application |
Learn how to configure a sample JavaScript single-page application (SPA) to sign in and sign out users. |
OwenRichards1 |
CelesteDG |
owenrichards |
entra-external-id |
customers |
devx-track-js |
sample |
04/10/2024 |
This guide uses a sample vanilla JavaScript (JS) single-page Application (SPA) to demonstrate how to add authentication to a SPA. The 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\0-sign-in-vanillajs\App
-
Install the project dependencies:
npm install
-
Open
App/public/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.
-
Open a new terminal and run the following command to start your express web server.
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 as shown in the following screenshot.
-
The SPA will now display a button saying Request Profile Information. Select it to display profile data.
:::image type="content" source="media/how-to-spa-vanillajs-sign-in-sign-in-out/display-vanillajs-welcome.png" alt-text="Screenshot of sign in into a vanilla JS SPA." lightbox="media/how-to-spa-vanillajs-sign-in-sign-in-out/display-vanillajs-welcome.png":::
- To sign out of the application, select Sign out in the navigation bar.
- A window appears asking which account to sign out of.
- Upon successful sign out, a final window appears advising you to close all browser windows.