Adding MSAL And React

Install MSAL by opening a console in UI project and installing MSAL

/azure-quickstart/ui/
npm install @azure/msal-browser

Modify the launch.json file of the UI project to add reference to a .env file which we can use to store secrets use for development but which we don't want to store in source control.

/azure-quickstart/ui/launch.json
{
  ...
  "configurations": [
    {
    ...
      "envFile": "${workspaceRoot}/.env",
    ...
    }
  ]
  ...
}

Crate a new fie .env and place your AAD Client Secret into it

IMPORTANT!! Be sure to exclude .env from GIT by adding .gitignore

/azure-quickstart/ui/.env
REACT_APP_FUNCTION_ENDPOINT=http://localhost:7071
REACT_APP_AAD_APP_CLIENTID=<<YOUR CLIENT ID>>
REACT_APP_AAD_DIRECTORY_TENANTID=<<YOUR TENANT ID>>
REACT_APP_AAD_REDIRECT_URI=http://localhost:5000
REACT_APP_AAD_SCOPES=api://sharethrift<<random number>>.com/access_as_user

Now we'll add a Provider to access MSAL, this will consist of a few files:

Lets start by creating the additional directory structure:

Under : /azure-quickstart/ui/src/ create the following directories : /components/core/msal

Next let's create each of the files in /azure-quickstart/ui/src/components/core/msal

Lets now reference this

We're going to edit ui/src/index.tsx

add a new import for msal, MsalProvider and the MsalProviderPopupConfig and add the config value

Wrap the App Component with the MsalProvider Component

Now lets edit the App.tsx and use the login context:

And add the following lines under the Edit and save and to reload.

Last updated

Was this helpful?