- Overview
- Scenario
- Prerequisites
- Registration
- Deployment
- Explore the sample
- More information
- Community Help and Support
- Contributing
- Code of Conduct
This sample demonstrates how to deploy a React single-page application (SPA) coupled with a Node.js web API to Azure Cloud using Azure Storage and Azure App Service, respectively.
Azure Storage provides a low cost static website hosting alternative. However, these static websites do not have advanced routing capabilities. As such, the React SPA in this tutorial does not have a router component.
For React applications with routing support, you can use Azure Static Web Apps instead. See Static Web App Deployment in the next section.
- The client application uses MSAL React to sign-in a user and obtain a JWT Access Token from Azure AD.
- The Access Token is used as a bearer token to authorize the user to call the protected web API.
- The protected web API responds with the claims in the Access Token.
- VS Code Azure Tools Extension extension is recommended for interacting with Azure through VS Code interface.
- An Azure subscription. This sample uses Azure Storage and Azure App Service.
From your shell or command line:
git clone https://github.com/Azure-Samples/ms-identity-javascript-react-tutorial.git
or download and extract the repository .zip file.
⚠️ To avoid path length limitations on Windows, we recommend cloning into a directory near the root of your drive.
- Setup the service app:
cd ms-identity-javascript-react-tutorial
cd 4-Deployment/1-deploy-storage
cd API
npm install
- Setup the client app:
cd ..
cd SPA
npm install
There are two projects in this sample. Each needs to be separately registered in your Azure AD tenant. To register these projects, you can:
-
follow the steps below for manually register your apps
-
or use PowerShell scripts that:
- automatically creates the Azure AD applications and related objects (passwords, permissions, dependencies) for you.
- modify the projects' configuration files.
Expand this section if you want to use this automation:
⚠️ If you have never used Microsoft Graph PowerShell before, we recommend you go through the App Creation Scripts Guide once to ensure that your environment is prepared correctly for this step.-
On Windows, run PowerShell as Administrator and navigate to the root of the cloned directory
-
In PowerShell run:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope Process -Force
-
Run the script to create your Azure AD application and configure the code of the sample application accordingly.
-
For interactive process -in PowerShell, run:
cd .\AppCreationScripts\ .\Configure.ps1 -TenantId "[Optional] - your tenant id" -AzureEnvironmentName "[Optional] - Azure environment, defaults to 'Global'"
Other ways of running the scripts are described in App Creation Scripts guide. The scripts also provide a guide to automated application registration, configuration and removal which can help in your CI/CD scenarios.
To manually register the apps, as a first step you'll need to:
- Sign in to the Azure portal.
- If your account is present in more than one Azure AD tenant, select your profile at the top right corner in the menu on top of the page, and then switch directory to change your portal session to the desired Azure AD tenant.
- Navigate to the Azure portal and select the Azure Active Directory service.
- Select the App Registrations blade on the left, then select New registration.
- In the Register an application page that appears, enter your application's registration information:
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
msal-node-api
. - Under Supported account types, select Accounts in this organizational directory only
- Select Register to create the application.
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
- In the Overview blade, find and note the Application (client) ID. You use this value in your app's configuration file(s) later in your code.
- In the app's registration screen, select the Expose an API blade to the left to open the page where you can publish the permission as an API for which client applications can obtain access tokens for. The first thing that we need to do is to declare the unique resource URI that the clients will be using to obtain access tokens for this API. To declare an resource URI(Application ID URI), follow the following steps:
- Select Set next to the Application ID URI to generate a URI that is unique for this app.
- For this sample, accept the proposed Application ID URI (
api://{clientId}
) by selecting Save. Read more about Application ID URI at Validation differences by supported account types (signInAudience).
- All APIs must publish a minimum of one scope, also called Delegated Permission, for the client apps to obtain an access token for a user successfully. To publish a scope, follow these steps:
- Select Add a scope button open the Add a scope screen and Enter the values as indicated below:
- For Scope name, use
access_as_user
. - Select Admins and users options for Who can consent?.
- For Admin consent display name type in access_as_user.
- For Admin consent description type in e.g. Allows the app to read the signed-in user's files..
- For User consent display name type in scopeName.
- For User consent description type in eg. Allows the app to read your files..
- Keep State as Enabled.
- Select the Add scope button on the bottom to save this scope.
- For Scope name, use
- Select the Manifest blade on the left.
- Set
accessTokenAcceptedVersion
property to 2. - Select on Save.
- Set
ℹ️ Follow the principle of least privilege whenever you are publishing permissions for a web API.
Open the project in your IDE (like Visual Studio or Visual Studio Code) to configure the code.
In the steps below, "ClientID" is the same as "Application ID" or "AppId".
- Open the
API\config.json
file. - Find the key
Enter_the_Application_Id_Here
and replace the existing value with the application ID (clientId) ofmsal-node-api
app copied from the Azure portal. - Find the key
Enter_the_Tenant_Info_Here
and replace the existing value with your Azure AD tenant/directory ID.
:information_source: Below, we are using a single app registration for both SPA and web API projects.
- Navigate to the Azure portal and select the Azure Active Directory service.
- Select the App Registrations blade on the left, then select New registration.
- In the Register an application page that appears, enter your application's registration information:
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
msal-react-spa
. - Under Supported account types, select Accounts in this organizational directory only
- Select Register to create the application.
- In the Name section, enter a meaningful application name that will be displayed to users of the app, for example
- In the Overview blade, find and note the Application (client) ID. You use this value in your app's configuration file(s) later in your code.
- In the app's registration screen, select the Expose an API blade to the left to open the page where you can publish the permission as an API for which client applications can obtain access tokens for. The first thing that we need to do is to declare the unique resource URI that the clients will be using to obtain access tokens for this API. To declare an resource URI(Application ID URI), follow the following steps:
- Select Set next to the Application ID URI to generate a URI that is unique for this app.
- For this sample, accept the proposed Application ID URI (
api://{clientId}
) by selecting Save. Read more about Application ID URI at Validation differences by supported account types (signInAudience).
- All APIs must publish a minimum of one scope, also called Delegated Permission, for the client apps to obtain an access token for a user successfully. To publish a scope, follow these steps:
- Select the Manifest blade on the left.
- Set
accessTokenAcceptedVersion
property to 2. - Select on Save.
- Set
ℹ️ Follow the principle of least privilege whenever you are publishing permissions for a web API.
- Since this app signs-in users, we will now proceed to select delegated permissions, which is is required by apps signing-in users.
- In the app's registration screen, select the API permissions blade in the left to open the page where we add access to the APIs that your application needs:
- Select the Add a permission button and then:
- Ensure that the My APIs tab is selected.
- In the list of APIs, select the API
msal-node-api
.- Since this app signs-in users, we will now proceed to select delegated permissions, which is is requested by apps when signing-in users.
- In the Delegated permissions section, select access_as_user in the list. Use the search box if necessary.
- Since this app signs-in users, we will now proceed to select delegated permissions, which is is requested by apps when signing-in users.
- Select the Add permissions button at the bottom.
Open the project in your IDE (like Visual Studio or Visual Studio Code) to configure the code.
In the steps below, "ClientID" is the same as "Application ID" or "AppId".
- Open the
SPA\src\authConfig.js
file. - Find the key
Enter_the_Application_Id_Here
and replace the existing value with the application ID (clientId) ofmsal-react-spa
app copied from the Azure portal. - Find the key
Enter_the_Tenant_Info_Here
and replace the existing value with your Azure AD tenant/directory ID. - Find the key
Enter_the_Web_Api_Application_Id_Here
and replace the existing value with the application ID (clientId) ofmsal-node-api
app copied from the Azure portal.
There are basically 3 stages that you will have to go through in order to deploy your projects and enable authentication:
- Upload your project files to Azure services and obtain published website URIs
- Update Azure AD App Registration with URIs you have just obtained
- Update your configuration files with URIs you have just obtained
There are various ways to deploy your applications to Azure App Service. Here we provide steps for deployment via VS Code Azure Tools Extension. For more alternatives, visit: Static website hosting in Azure Storage.
We recommend watching the video tutorial offered by Microsoft Docs for preparation.
- In the VS Code activity bar, select the Azure logo to show the AZURE APP SERVICE explorer. Select Sign in to Azure... and follow the instructions. Once signed in, the explorer should show the name of your Azure subscription(s).
- On the App Service explorer section you will see an upward-facing arrow icon. Click on it publish your local files in the
API
folder to Azure App Services.
- Choose a creation option based on the operating system to which you want to deploy. in this sample, we choose Linux.
- Select a Node.js version when prompted. An LTS version is recommended.
- Type a globally unique name for your web app and press Enter. The name must be unique across all of Azure.
- After you respond to all the prompts, VS Code shows the Azure resources that are being created for your app in its notification popup.
- Select Yes when prompted to update your configuration to run npm install on the target Linux server.
Now you need to navigate to the Azure App Service Portal, and locate your project there. Once you do, click on the Authentication/Authorization blade. There, make sure that the App Services Authentication is switched off (and nothing else is checked), as we are using our own authentication logic.
There are various ways to deploy your applications to Azure Storage. Here we provide steps for deployment via VS Code Azure Tools Extension. For more alternatives, visit: Static website hosting in Azure Storage.
We recommend watching the video tutorial offered by Microsoft Docs for preparation.
- Create a distributable files folder, where your
html
,css
andjavascript
files will be located. To do so, locate theSPA
folder in your terminal, then type:
npm run build
This will create a build
folder. We will upload the contents of this folder next.
- Right click on the
SPA/build
folder. This will open a context menu where you will see the option Deploy to static website via Azure Storage. Click on it.
- Follow the dialog window that opens on the top. Select your subscription, then give a name to your storage account.
- Once your storage account is created and your files are uploaded, you will see a notification on the bottom-right corner of VS Code interface. When it's done, you will be notified with the published URI of your static website (e.g.
https://reactspa1.z22.web.core.windows.net/
).
- Navigate back to to the Azure Portal.
- In the left-hand navigation pane, select the Azure Active Directory service, and then select App registrations.
- In the resulting screen, select the name of your application.
- From the Branding menu, update the Home page URL, to the address of your service, for example https://reactspa1.z22.web.core.windows.net/. Save the configuration.
- Add the same URI in the list of values of the Authentication -> Redirect URIs menu. If you have multiple redirect URIs, make sure that there a new entry using the App service's URI for each redirect URI.
Now you need to update your authentication configuration files in the client project. To do so, go to your Azure Storage Account explorer via VS Code Azure panel. There, click on your project's name > Blob Container > Web as shown below:
Open authConfig.js
. Then:
- Find the key
protectedResources.apiHello.endpoint
and replace the existing value with your published web API's endpoint, e.g.https://my-web-api.azurewebsites.net/hello
Finally, we need to enable Cross-Origin Resource Sharing by designating the domain of the SPA we've just deployed. Navigate to App Service portal and locate your web API. Then, enable CORS by entering your website's URI as shown below:
- Open your browser and navigate to your deployed client app's URI, for instance:
https://reactspa1.z22.web.core.windows.net/
. - Select the Sign In button on the top right corner. Choose either Popup or Redirect flow.
- Select the Call the API button in the home screen. This will make a call to your web API.
Were we successful in addressing your learning objective? Consider taking a moment to share your experience with us.
For more information about how OAuth 2.0 protocols work in this scenario and other scenarios, see Authentication Scenarios for Azure AD.
Use Stack Overflow to get support from the community.
Ask your questions on Stack Overflow first and browse existing issues to see if someone has asked your question before.
Make sure that your questions or comments are tagged with [azure-ad
azure-ad-b2c
ms-identity
msal
].
If you find a bug in the sample, please raise the issue on GitHub Issues.
To provide a recommendation, visit the following User Voice page.
If you'd like to contribute to this sample, see CONTRIBUTING.MD.
This project has adopted the Microsoft Open Source Code of Conduct. For more information, see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact [email protected] with any additional questions or comments.