Skip to content

Micro front ends — auth ​

Overview ​

This guide will walk you through the process of setting up authentication for your micro frontends. For context, if you are running a micro frontend locally using yarn start - by default this will run independently from the host app. This means that the micro frontend will not be able to access the host app's services as authentication will fail.

Adding Authentication to the Micro Frontend ​

To add authentication to a micro frontend application, you can follow these steps:

  1. If your micro frontend does not have a start script, add one to your package.json's scripts section:

The port is configured in the micro frontend's vite.config.ts (server.port), not passed as a CLI flag.

json
"start": "STANDALONE=true vite",
  1. Wrap your application in the HostAuthProvider component:
jsx
import { HostAuthProvider } from 'host/authProvider';

function App() {
  return (
+   <HostAuthProvider>
      <h1>MFA App</h1>
+   </HostAuthProvider>
  );
}

export default App;
  1. In the host app (luna-front-end), you will need to add the MFA origin to the allowedMfaOrigins array in the clients/luna-front-end/src/routes/AuthenticatedRoutes.jsx file:
jsx
const allowedMfaOrigins = [
  process.env.VITE_APP_ADMIN_DOMAIN,
  process.env.VITE_APP_PORTAL_DOMAIN,
  process.env.VITE_APP_KUDOS_DOMAIN,
  process.env.VITE_APP_STABLE_APPS_DOMAIN,
+ process.env.VITE_APP_NEW_MFA_DOMAIN,
].map(origin => `${process.env.VITE_APP_PROTOCOL}://${origin}`);

Replace VITE_APP_NEW_MFA_DOMAIN with the environment variable for the micro frontend you are working on. If it is a new micro frontend, you will need to set this in the .env.local file for the host app (make sure to also add it to the .env.local.sample file).

  1. If your micro frontend is connecting to an API service, you will need to update the application.yml file of the service(s) to include the micro frontend origin in the AllowOrigins section of the HttpApi resource:

Replace 3001 with the port for the micro frontend you are working on.

yaml
AllowOrigins:
  - !FindInMap [FrontendDomainMap, !Ref Environment, Domain]
  - !If [
      IsLowerEnvironment,
      'http://localhost:3000',
      !Ref 'AWS::NoValue',
    ]
+ - !If [
+     IsLowerEnvironment,
+     'http://localhost:3001',
+     !Ref 'AWS::NoValue',
+   ]
  1. If your micro frontend has multiple pages, you can use the MFARoutes component (from the host app) to render a simple navigation bar and routes when running the MFA locally:
jsx
import { HostAuthProvider, MFARoutes } from 'host/authProvider';
import Items from './pages/Items';
import ViewItem from './pages/ViewItem';

const routes = [
  { path: '/items', element: <Items /> },
  { path: '/items/:id', element: <ViewItem /> },
];

function App() {
  return (
    <HostAuthProvider>
      <h1>MFA App</h1>
+     <MFARoutes routes={routes} />
    </HostAuthProvider>
  );
}

export default App;

Running the Host App and Micro Frontend Locally ​

Now you've added authentication to your micro frontend, you can run the host app and micro frontend locally.

The recommended way to run the host together with all micro frontends is via the luna CLI: run luna mfa install then luna mfa dev. This starts the host and every micro frontend together at localhost:3000 with hot reloading across both. The manual steps below remain a fallback if you need to run the host and a single micro frontend separately.

  1. Build the host app in dev mode (watches for changes):
bash
yarn dev
  1. Run the host app in preview mode (serves the built files from the dev step):
bash
yarn preview
  1. Run the micro frontend using HMR (Hot Module Replacement):
bash
yarn start

You will now be able to access the host app at http://localhost:3000 and the micro frontend at http://localhost:3001 (or the port configured for the micro frontend you are working on).

If you want to see your changes to the micro frontend inside the host app (e.g. with full routing, themes etc.) then you will need to run yarn dev and yarn preview in the micro frontend app (instead of yarn start).

Made with ❤️ by the Luna team