question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MsalProvider with single-spa and multiple applications

See original GitHub issue

Core Library

MSAL.js v2 (@azure/msal-browser)

Core Library Version

2.19.0

Wrapper Library

MSAL React (@azure/msal-react)

Wrapper Library Version

1.1.1

Description

We are using React and single-spa to implement our multi-frontend application. We have a navigation bar and an index part; these are separated into two different applications. Parts of these applications should be shown (or not) depending on the authentication of the user.

The problem is that we can’t figure out how to use the mandatory <MsalProvider> in a way that it wraps around our application. Right now we use <MsalProvider> twice, which we know is not recommended and leads to the problem that the instance state is not synchronized among our microfrontends: One application works as expected, whereas the other’s instance’s context is not updated correctly. Refreshing the page or navigating to it results in unexpected behaviour.

Is there a way to use msal-react with single-spa while using multiple applications?

The user in the aformentioned issue solved his problem by using the vanilla javascript library.

Another user had a similar problem that was also linked to using <MsalProvider> in an inappropriate way; This seems to be related to an entirely different problem though.

MSAL Configuration

auth: {
        clientId,
        authority: '<my-authority>',
        knownAuthorities: ['<my-known-authority>'],
        redirectUri: '/',
        postLogoutRedirectUri: '/',
        navigateToLoginRequestUrl: false,
    },
    cache: {
        cacheLocation: 'localStorage',
        storeAuthStateInCookie: false,
    },

Relevant Code Snippets

// microfrontend-layout.html
<single-spa-router>
    <div>
        <div id="header"></div>
        <div>
            <div id="index"></div>
        </div>
    </div>
</single-spa-router>



// create_application.ts
import { registerApplication, start } from 'single-spa';
import { constructApplications, constructRoutes, constructLayoutEngine } from 'single-spa-layout';

import microfrontendLayout from './microfrontend-layout.html';

import msalInstance from './authenticationInstance';

const routes = constructRoutes(microfrontendLayout);
const applications = constructApplications({
    routes,
    loadApp({ name }) {
        return System.import(name);
    },
});

registerApplication(
    'navbar',
    () => import('./navbar/index.app').then((module) => module.default),
    () => true,
    { msalInstance },
);

registerApplication(
    'index',
    () => import('./index/index.app').then((module) => module.default),
    (location) => location.pathname === '/index.html' || location.pathname === '/',
    { msalInstance },
);

const layoutEngine = constructLayoutEngine({ routes, applications, active: false });

applications.forEach(registerApplication);
layoutEngine.activate();
start();



// navbar/index.app.tsx (similar to index/index.app.tsx, therefore we use MsalProvider twice)
import * as React from 'react';
import ReactDOM from 'react-dom';
import { MsalProvider } from '@azure/msal-react';
import singleSpaReact from 'single-spa-react';
import Navbar from './Navbar';

export default singleSpaReact({
    React,
    ReactDOM,
    rootComponent: (props) => (
        <MsalProvider instance={props.msalInstance}>
            <Navbar instance={props.msalInstance} />
        </MsalProvider>
    ),
    domElementGetter: () => document.getElementById('header'),
});

Identity Provider

Azure B2C Basic Policy

Source

External (Customer)

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
exaucaecommented, Nov 25, 2021
1reaction
yosheggcommented, Nov 19, 2021

@exaucae @samuelkubai We will try that and post our results. Thank you for your time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Single-SPA + MSAL - Stack Overflow
Azure Active Directory: Difference between adding multiple platforms as Web and SPA to single applications instead of an application for each?
Read more >
Configure single-page app - Microsoft Entra
Learn how to configure the code for your single-page application (SPA). Microsoft libraries supporting single-page apps. The following Microsoft ...
Read more >
Frequently Asked Questions - Single SPA
The applications can all be written in the same framework, or they can be implemented in different frameworks. Is there a recommended setup?​....
Read more >
Working with MSAL and multiple Azure AD accounts in a ...
Before looking at the code, we would need to create a multi tenant Azure AD app which ... import { MsalProvider } from...
Read more >
Using MSAL in a React SPA with Keycloak - Frode Hus
We need to wrap our application with our MSAL Provider so it is available to any pages within it. function MyApp({ Component, pageProps...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found