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:
- If your micro frontend does not have a
startscript, add one to yourpackage.json'sscriptssection:
The port is configured in the micro frontend's
vite.config.ts(server.port), not passed as a CLI flag.
"start": "STANDALONE=true vite",- Wrap your application in the
HostAuthProvidercomponent:
import { HostAuthProvider } from 'host/authProvider';
function App() {
return (
+ <HostAuthProvider>
<h1>MFA App</h1>
+ </HostAuthProvider>
);
}
export default App;- In the host app (luna-front-end), you will need to add the MFA origin to the
allowedMfaOriginsarray in theclients/luna-front-end/src/routes/AuthenticatedRoutes.jsxfile:
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_DOMAINwith 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.localfile for the host app (make sure to also add it to the.env.local.samplefile).
- If your micro frontend is connecting to an API service, you will need to update the
application.ymlfile of the service(s) to include the micro frontend origin in theAllowOriginssection of theHttpApiresource:
Replace
3001with the port for the micro frontend you are working on.
AllowOrigins:
- !FindInMap [FrontendDomainMap, !Ref Environment, Domain]
- !If [
IsLowerEnvironment,
'http://localhost:3000',
!Ref 'AWS::NoValue',
]
+ - !If [
+ IsLowerEnvironment,
+ 'http://localhost:3001',
+ !Ref 'AWS::NoValue',
+ ]- If your micro frontend has multiple pages, you can use the
MFARoutescomponent (from the host app) to render a simple navigation bar and routes when running the MFA locally:
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
lunaCLI: runluna mfa installthenluna mfa dev. This starts the host and every micro frontend together atlocalhost:3000with hot reloading across both. The manual steps below remain a fallback if you need to run the host and a single micro frontend separately.
- Build the host app in
devmode (watches for changes):
yarn dev- Run the host app in
previewmode (serves the built files from thedevstep):
yarn preview- Run the micro frontend using HMR (Hot Module Replacement):
yarn startYou 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 devandyarn previewin the micro frontend app (instead ofyarn start).
