Skip to content
This repository was archived by the owner on May 3, 2024. It is now read-only.

Latest commit

 

History

History
399 lines (296 loc) · 21.3 KB

File metadata and controls

399 lines (296 loc) · 21.3 KB
page_type name description languages products urlFragment extensions
sample
React single-page application using MSAL React to sign-in users against Azure AD B2C
React single-page application using MSAL React to sign-in users against Azure AD B2C
javascript
azure-active-directory-b2c
msal-js
msal-react
ms-identity-javascript-react-tutorial
services
ms-identity
platform
JavaScript
endpoint
AAD v2.0
level
100
client
React SPA

React single-page application using MSAL React to sign-in users against Azure AD B2C

Overview

This sample demonstrates a React single-page application (SPA) authenticating users against Azure AD B2C (Azure AD), using the Microsoft Authentication Library for React (MSAL React).

MSAL React is a wrapper around the Microsoft Authentication Library for JavaScript (MSAL.js). As such, it exposes the same public APIs that MSAL.js offers, while adding many new features customized for modern React applications.

Here you'll learn about authentication and B2C concepts, such as ID tokens, external identity providers, consumer social accounts, single-sign on (SSO) and more.

Scenario

  1. The client React SPA uses MSAL React to obtain an ID Token from Azure AD B2C.
  2. The ID Token proves that the user has successfully authenticated against Azure AD B2C.

Scenario Image

Contents

File/folder Description
App.jsx Main application logic resides here.
authConfig.js Contains authentication configuration parameters.
pages/Home.jsx Contains a table with ID token claims and description

Prerequisites

Setup the sample

Step 1: Clone or download this repository

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.

Step 2: Install project dependencies

    cd ms-identity-javascript-react-tutorial
    cd 1-Authorization\2-sign-in-b2c\SPA
    npm install

Step 3: Register the sample application(s) in your tenant

⚠️ This sample comes with a pre-registered application for demo purposes. If you would like to use your own Azure AD B2C tenant and application, follow the steps below to register and configure the application on Azure portal. Otherwise, continue with the steps for Running the sample.

  • follow the steps below for manually register your apps

Choose the Azure AD B2C tenant where you want to create your applications

To manually register the apps, as a first step you'll need to:

  1. Sign in to the Azure portal.
  2. If your account is present in more than one Azure AD B2C 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 B2C tenant.

Create User Flows and Custom Policies

Please refer to: Tutorial: Create userflows in Azure Active Directory B2C

⚠️ This sample requires Azure AD B2C to emit the emails claim in the ID token, which is used as username by MSAL. To do so, navigate to the Azure portal and locate the Azure AD B2C service. Then, navigate to the User flows blade. Select the User Attributes tab and make sure Email Address is checked. Then select the Application Claims tab and make sure Email Addresses is checked.

You may want additional claims (such as object ID (oid) and etc.) to appear in the ID tokens obtained from Azure AD B2C user-flows. In that case, please refer to User profile attributes to learn about how to configure your user-flows to emit those claims.

Add External Identity Providers

Please refer to: Tutorial: Add identity providers to your applications in Azure Active Directory B2C

Register the client app (ms-identity-react-c1s2)

  1. Navigate to the Azure portal and select the Azure Active Directory B2C service.
  2. Select the App Registrations blade on the left, then select New registration.
  3. In the Register an application page that appears, enter your application's registration information:
    1. In the Name section, enter a meaningful application name that will be displayed to users of the app, for example ms-identity-react-c1s2.
    2. Under Supported account types, select Accounts in any identity provider or organizational directory (for authenticating users with user flows)
    3. Select Register to create the application.
  4. 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.
  5. In the app's registration screen, select the Authentication blade to the left.
  6. If you don't have a platform added, select Add a platform and select the Single-page application option.
    1. In the Redirect URI section enter the following redirect URIs:
      1. http://localhost:3000
      2. http://localhost:3000/redirect
    2. Click Save to save your changes.
Configure the client app (ms-identity-react-c1s2) to use your app registration

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".

  1. Open the SPA\src\authConfig.js file.
  2. Find the key msalConfig.auth.clientId and replace the existing value with the application ID (clientId) of ms-identity-react-c1s2 app copied from the Azure portal.

To setup your B2C user-flows, do the following:

  1. Find the key names and populate it with your policy names e.g. signUpSignIn.
  2. Find the key authorities and populate it with your policy authority strings e.g. https://<your-tenant-name>.b2clogin.com/<your-tenant-name>.onmicrosoft.com/b2c_1_susi.
  3. Find the key authorityDomain and populate it with the domain portion of your authority string e.g. <your-tenant-name>.b2clogin.com.

Step 4: Running the sample

    cd 1-Authorization\2-sign-in-b2c\SPA
    npm start

Explore the sample

  1. Open your browser and navigate to http://localhost:3000.
  2. Select the Sign In button on the top right corner. Choose either Popup or Redirect flows (see: MSAL.js interaction types).
    1. During the sign-in screen, you may select forgot my password. This initiates the Azure AD B2C password reset user-flow.
  3. Select the Edit Profile button on the top right corner. This initiates the Azure AD B2C edit profile user-flow using a popup window (hint: alternatively, you may use redirect flow here instead). Screenshot

ℹ️ Did the sample not work for you as expected? Then please reach out to us using the GitHub Issues page.

ℹ️ if you believe your issue is with the B2C service itself rather than with the sample, please file a support ticket with the B2C team by following the instructions here.

We'd love your feedback!

Were we successful in addressing your learning objective? Consider taking a moment to share your experience with us.

Troubleshooting

Expand for troubleshooting info

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-active-directory react ms-identity adal msal].

About the code

MSAL React should be instantiated outside of the component tree to prevent it from being re-instantiated on re-renders. After instantiation, pass it as props to your application. This is illustrated in index.js.

const msalInstance = new PublicClientApplication(msalConfig);

ReactDOM.render(
    <React.StrictMode>
        <App msalInstance={msalInstance}/>
    </React.StrictMode>,
    document.getElementById("root")
);

This is illustrated in App.jsx.

export default function App({msalInstance}) {

    return (
        <MsalProvider instance={msalInstance}>
            <PageLayout>
                <MainContent />
            </PageLayout>
        </MsalProvider>
    );
}

At the top of your component tree, wrap everything between MsalProvider component. All components underneath MsalProvider will have access to the PublicClientApplication instance via context as well as all hooks and components provided by msal-react. This is illustrated in App.jsx.

export default function App({msalInstance}) {

    return (
        <MsalProvider instance={msalInstance}>
            <PageLayout>
                <MainContent />
            </PageLayout>
        </MsalProvider>
    );
}

Sign-in

MSAL.js exposes 3 login APIs: loginPopup(), loginRedirect() and ssoSilent(). These APIs are usable in MSAL React as well:

    export function App() {
        const { instance, accounts, inProgress } = useMsal();
    
        if (accounts.length > 0) {
            return <span>There are currently {accounts.length} users signed in!</span>
        } else if (inProgress === "login") {
            return <span>Login is currently in progress!</span>
        } else {
            return (
                <>
                    <span>There are currently no users signed in!</span>
                    <button onClick={() => instance.loginPopup()}>Login</button>
                </>
            );
        }
    }

You may also use MSAL React's useMsalAuthentication hook. Below is an example in which the ssoSilent() API is used. When using ssoSilent(), the recommended pattern is that you fallback to an interactive method should the silent SSO attempt fails:

function App() {
    const request = {
        loginHint: "[email protected]",
        scopes: ["User.Read"]
    }

    const { login, result, error } = useMsalAuthentication(InteractionType.Silent, request);

    useEffect(() => {
        if (error) {
            login(InteractionType.Popup, request);
        }
    }, [error]);

    const { accounts } = useMsal();

    return (
        <React.Fragment>
            <p>Anyone can see this paragraph.</p>
            <AuthenticatedTemplate>
                <p>Signed in as: {accounts[0]?.username}</p>
            </AuthenticatedTemplate>
            <UnauthenticatedTemplate>
                <p>No users are signed in!</p>
            </UnauthenticatedTemplate>
        </React.Fragment>
    );
}

As shown above, the components that depend on whether the user is authenticated should be wrapped inside React's AuthenticatedTemplate and UnauthenticatedTemplate components. Alternatively, you may use the useIsAuthenticated hook to conditionally render components.

Sign-out

The application redirects the user to the Microsoft identity platform logout endpoint to sign out. This endpoint clears the user's session from the browser. If your app did not go to the logout endpoint, the user may re-authenticate to your app without entering their credentials again, because they would have a valid single sign-in session with the Microsoft identity platform endpoint. See for more: Send a sign-out request.

The sign-out clears the user's single sign-on session with Azure AD B2C, but it might not sign the user out of their social identity provider session. If the user selects the same identity provider during a subsequent sign-in, they might re-authenticate without entering their credentials. Here the assumption is that, if a user wants to sign out of the application, it doesn't necessarily mean they want to sign out of their social account (e.g. Facebook) itself.

ID Token Validation

When you receive an ID token directly from the IdP on a secure channel (e.g. HTTPS), such is the case with SPAs, there’s no need to validate it. If you were to do it, you would validate it by asking the same server that gave you the ID token to give you the keys needed to validate it, which renders it pointless, as if one is compromised so is the other.

Events API

Using the event API, you can register an event callback that will do something when an event is emitted. When registering an event callback in a react component you will need to make sure you do 2 things.

  1. The callback is registered only once
  2. The callback is unregistered before the component unmounts.

Here, we use the event API when integrating the B2C user-flows (discussed below).

Integrating user-flows

  • Sign-up/sign-in

This user-flow allows your users to sign-in to your application if the user has an account already, or sign-up for an account if not. This is the default user-flow that we pass during the initialization of MSAL instance.

  • Password reset

When a user clicks on the forgot your password? link during sign-in, Azure AD B2C will throw an error. To initiate the password reset user-flow, you need to catch this error and handle it by sending another login request with the corresponding password reset authority string. This is illustrated in App.jsx.

if (event.eventType === EventType.LOGIN_FAILURE) {
    // Check for forgot password error
    // Learn more about AAD error codes at https://docs.microsoft.com/en-us/azure/active-directory/develop/reference-aadsts-error-codes
    if (event.error && event.error.errorMessage.includes('AADB2C90118')) {
        const resetPasswordRequest = {
            authority: b2cPolicies.authorities.forgotPassword.authority,
            scopes: [],
        };
        instance.loginRedirect(resetPasswordRequest);
    }
}

We need to reject ID tokens that were not issued with the default sign-in policy. After the user resets her password and signs-in again, we will force the user to login again(with the default sign-in policy). This is illustrated in App.jsx.

/**
* Below we are checking if the user is returning from the reset password flow.
* If so, we will ask the user to reauthenticate with their new password.
* If you do not want this behavior and prefer your users to stay signed in instead,
* you can replace the code below with the same pattern used for handling the return from
* profile edit flow
*/
if (event.payload.idTokenClaims['tfp'] === b2cPolicies.names.forgotPassword) {
    let signUpSignInFlowRequest = {
        authority: b2cPolicies.authorities.signUpSignIn.authority,
    };
    instance.loginRedirect(signUpSignInFlowRequest);
}
  • Edit Profile

When a user selects the Edit Profile button on the navigation bar, we simply initiate a sign-in flow. Like password reset, edit profile user-flow requires users to sign-out and sign-in again. This is illustrated in App.jsx.

/**
 * For the purpose of setting an active account for UI update, we want to consider only the auth
 * response resulting from SUSI flow. "tfp" claim in the id token tells us the policy (NOTE: legacy
 * policies may use "acr" instead of "tfp"). To learn more about B2C tokens, visit:
 * https://docs.microsoft.com/en-us/azure/active-directory-b2c/tokens-overview
*/
if (event.payload.idTokenClaims['tfp'] === b2cPolicies.names.editProfile) {
    // retrieve the account from initial sing-in to the app
    const originalSignInAccount = instance
            .getAllAccounts()
            .find(
                (account) =>
                     account.idTokenClaims.oid === event.payload.idTokenClaims.oid &&
                    account.idTokenClaims.sub === event.payload.idTokenClaims.sub &&
                    account.idTokenClaims['tfp'] === b2cPolicies.names.signUpSignIn
                );

    let signUpSignInFlowRequest = {
        authority: b2cPolicies.authorities.signUpSignIn.authority,
        account: originalSignInAccount,
    };
    // silently login again with the signUpSignIn policy
    instance.ssoSilent(signUpSignInFlowRequest);
}    

Next Steps

Learn how to:

Contributing

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.

Learn More