3

I’ve got a ReactJS website in which I am trying to use "@azure/msal-react": "^1.0.0-beta.1", and ran into some issues that have me flummoxed.

  1. Most of my pages are open to the public. Some require login. If I add the MSALAuthenticationTemplate as below (but with interactionType=Redirect), as soon as I start the site, it asks me to login. I thought it would only do that if I hit a route that was in the AuthenticationTemplate.

  2. Using InteractionType Popup causes the SPA to throw an exception on startup Error: Objects are not valid as a React child (found: object with keys {login, result, error}). If you meant to render a collection of children, use an array instead. in p (at App.tsx:44)

  3. All of my routes are, for some reason, coming back to the home page instead of loading the relevant components, event with the AuthenticationTemplate commented out.

I had this pretty much working using straight Javascript, but was running into ESLint issues when publishing, so I thought Typescript would help me fix those. But now it’s just broke.

render() {
        initializeIcons();

        return (
            <MsalProvider instance={msalClient} >
                <div className="d-flex flex-column h-100">
                    <TopMenu />

                    <div className="container-fluid flex-grow-1 d-flex">
                        <div className="row flex-fill flex-column flex-sm-row">

                            <BrowserRouter>
                                <MsalAuthenticationTemplate
                                    interactionType={InteractionType.Popup}
                                    errorComponent={this.ErrorComponent}
                                    loadingComponent={this.LoadingComponent}>
                                    <Switch>
                                        <Route path="/addevent">
                                            <AddEvent />
                                        </Route>
                                        <Route path="/mydashboard">
                                            <MyDashboard />
                                        </Route>
                                    </Switch>
                                </MsalAuthenticationTemplate >
                                <UnauthenticatedTemplate>
                                    <Switch>
                                        <Route path='/'>
                                            <Home />
                                        </Route>
                                        <Route path="/about">
                                            <About />
                                        </Route>
                                        <Route path="/contactus">
                                            <ContactUs />
                                        </Route>
                                        <Route path="/faq">
                                            <Faq />
                                        </Route>
                                        <Route path="/fetchevents">
                                            <FetchEvents />
                                        </Route>
                                        <Route path="/gettingstarted">
                                            <GettingStarted />
                                        </Route>
                                        <Route path="/partners">
                                            <Partners />
                                        </Route>
                                        <Route path="/privacypolicy">
                                            <PrivacyPolicy />
                                        </Route>
                                        <Route path="/sponsors">
                                            <Sponsors />
                                        </Route>
                                        <Route path="/termsofservice">
                                            <TermsOfService />
                                        </Route>
                                        <Route path="/userstories">
                                            <UserStories />
                                        </Route>
                                    </Switch>
                                </UnauthenticatedTemplate>
                                <div>
                                    <Footer />
                                </div>
                            </BrowserRouter>
                        </div>
                    </div>
                </div>
            </MsalProvider>
        );
Joe B
  • 66
  • 1
  • 5

2 Answers2

3

Let's start with the UnauthenticatedTemplate. If the user is authenticated, children of the component will not show. So I guess you don't want to use it there. It's typical usage is for Login/Logout button for example.

Another problem is that if you are using MsalAuthenticationTemplate as the parent of the Switch and Route components. The problem is that you are guarding switch and routes from unauthenticated users, but this components should always be available without authentication, if you don't want to protect whole page.

During rendering React will go through your components one by one and first child of the BrowserRouter component it will try to render is MsalAuthenticationTemplate and since user is not authenticated, it will redirect user to login page.

This is quote from react-router docs:

A Route is always technically “rendered” even though it’s rendering null. When the 's path matches the current URL, it renders its children (your component).

Because of this the children of the route will only be rendered if the route will be hit. So you need to put MsalAuthenticationTemplate component as a direct child of the route, or even inside such component:

<Switch>
    <Route path="/addevent">
        <MsalAuthenticationTemplate
              interactionType={InteractionType.Redirect}
              authenticationRequest={loginRequest}
            >
            <AddEvent />
       </MsalAuthenticationTemplate>
    </Route>
    ...
</Switch>

As for all the webpages redirected to your home screen, you should add exact keyword to your Home route. This attribute causes it to not match all other routes also. Single '/' matches all your other routes.

DirectionUnkown
  • 203
  • 3
  • 9
0

In addition to the answer already provided, there is a way (cleaner in my opinion) you can configure MSAL react to take advantage of the router's navigate functions when MSAL redirects between pages in your app.

Here is how it works: In your index.js file you can have something like so:

import { PublicClientApplication, EventType } from "@azure/msal-browser";
import { msalConfig } from "./authConfig";

export const msalInstance = new PublicClientApplication(msalConfig);

ReactDOM.render(
    <React.StrictMode>
        <Router>
            <ThemeProvider theme={theme}>
                <App pca={msalInstance} />
            </ThemeProvider>
        </Router>
    </React.StrictMode>,
    document.getElementById('root')
);

As shown, you need to pass msal instance as props to your main App. Then in your App.js where you setup your routes, you will need to do the following:

import { MsalProvider } from "@azure/msal-react";
import { CustomNavigationClient } from "NavigationClient";
import { useHistory } from "react-router-dom";

function App({ pca }) {
  // The 3 lines of code below allow you to configure MSAL to take advantage of the router's navigate functions when MSAL redirects between pages in your app
  const history = useHistory();
  const navigationClient = new CustomNavigationClient(history);
  pca.setNavigationClient(navigationClient);

  return (
    <MsalProvider instance={pca}>
        <Grid container justify="center">
          <Pages />
        </Grid>
    </MsalProvider>
  );
}

function Pages() {
  return (
    <Switch>
      <Route path="/addevent">
         <AddEvent />
      </Route>
      <Route path="/mydashboard">
         <MyDashboard />
      </Route>
      <Route path='/'>
         <Home />
      </Route>
      <Route path="/about">
         <About />
      </Route>
      <Route path="/contactus">
         <ContactUs />
      </Route>
      <Route path="/faq">
         <Faq />
      </Route>
       // your other routes
    </Switch>
  )
}

And here is the helper function used in App.js that enables navigation by overriding the the default function used by MSAL

import { NavigationClient } from "@azure/msal-browser";

/**
 * This is an example for overriding the default function MSAL uses to navigate to other urls in your webpage
 */
export class CustomNavigationClient extends NavigationClient{
    constructor(history) {
        super();
        this.history = history;
    }

    /**
     * Navigates to other pages within the same web application
     * You can use the useHistory hook provided by react-router-dom to take advantage of client-side routing
     * @param url 
     * @param options 
     */
    async navigateInternal(url, options) {
        const relativePath = url.replace(window.location.origin, '');
        if (options.noHistory) {
            this.history.replace(relativePath);
        } else {
            this.history.push(relativePath);
        }

        return false;
    }
}

You can then use AuthenticatedTemplate on your private pages and UnauthenticatedTemplate on the public pages. For example if you have have addEvent.js (private) and Home.js (public), you will have each components like so:

export function Home() {
  return (
      <>
          <AuthenticatedTemplate>
            <p>Welcome Home - it's public</p>
          </AuthenticatedTemplate>
      </>
  );
}

export function AddEvent() {
  return (
      <>
          <UnauthenticatedTemplate>
            <Typography variant="h6">
                Add event -  it is a private page
            </Typography>
          </UnauthenticatedTemplate>
      </>
  );
}

Here is a complete example on how to use react-router with msal react for your reference.

Tee
  • 385
  • 3
  • 14