This project demonstrates a secure Module Federation setup with Authentik authentication and Nginx proxy. It creates a system where the Host application can access Remote components, but direct access to the Remote application is restricted.
- Host Application: Main application consuming remote components
- Remote Application: Exposes components via Module Federation
- Nginx: Reverse proxy controlling access and handling authentication
- Authentik: Identity provider for authentication
- Secure Module Federation with proxy authentication
- Restricted direct access to Remote components
- Custom styled error pages for unauthorized access attempts
- User authentication with Authentik
- User info sharing between applications
- Logout functionality
- Docker and Docker Compose
- Basic understanding of React, Module Federation, and authentication
- Sudo access (for setting up local hostnames)
git clone https://github.com/yourusername/mf-proxy-auth.git
cd mf-proxy-authRun the setup script to add necessary hostnames to your /etc/hosts file:
sudo ./setup-hosts.shThis adds:
127.0.0.1 authentik.local
127.0.0.1 api.local
127.0.0.1 host.local
127.0.0.1 remote.local
docker compose up -dWait for all services to start (approximately 1-2 minutes).
- Visit http://authentik.local/
- Create your admin account with a secure password
- Complete the account setup process
- Navigate to Admin Interface > Providers > Create
- Select Proxy Provider
- Fill in the form:
- Name:
Nginx Proxy - Authentication flow:
default-authentication-flow - Authorization flow:
default-provider-authorization-explicit-consent - External host:
http://authentik.local - Internal host:
http://authentik-server:9000 - Under Advanced Protocol Settings:
- Skip path regex:
^/$|^/api/|^/static/|^/.well-known/|^/outpost.goauthentik.io/
- Skip path regex:
- Name:
- Click Save
- Go to Admin Interface > Outposts > Create
- Fill in:
- Name:
Nginx Outpost - Type:
Proxy - Providers: Select the
Nginx Proxyprovider
- Name:
- Click Save
- Go to Admin Interface > Applications > Create
- Create the Host app:
- Name:
Host Application - Slug:
host-app - Provider: Select
Nginx Proxy
- Name:
- Click Save
- Repeat to create a Remote app with:
- Name:
Remote Application - Slug:
remote-app - Provider: Select
Nginx Proxy
- Name:
- Go to Admin Interface > Providers > Edit your Proxy Provider
- Add these headers:
- X-authentik-username:
user.username - X-authentik-email:
user.email - X-authentik-name:
user.name - X-authentik-groups:
user.groups
- X-authentik-username:
- Click Save
- Go to Admin Interface > Outposts
- Select your Nginx Outpost
- Click Deploy > Embedded Outpost
- Host application: http://host.local/
- Remote application: http://remote.local/ (will return 403 - only accessible through Host)
- Authentik portal: http://authentik.local/
- API endpoint: http://api.local/user-info
- When you visit http://host.local/, Nginx forwards the request to Authentik
- Authentik authenticates you (redirecting to login if needed)
- After authentication, Authentik passes user information back to Nginx
- Nginx adds user info as headers and proxies to the Host application
- The Host application reads user information and displays it
- The Host application loads components from the Remote application
- The Remote application checks the referer header to ensure requests come from the Host
- The Host application (
host.local) loads aRemoteComponentfrom the Remote application - The Remote application exposes components through Module Federation
- Authentication credentials and user info are passed between applications
- Direct access to Remote components is forbidden with custom error pages
- Authentik provides authentication and user management
- Nginx enforces access controls based on request origin
- Custom error pages guide users who try to access Remote directly
- Logout button for ending user sessions
- Check Authentik logs:
docker compose logs -f authentik-server - Verify Provider configuration in Authentik
- Make sure the Outpost is properly deployed and connected
- Verify hosts file entries:
cat /etc/hosts - Check Nginx logs:
docker compose logs -f nginx - Ensure all containers are running:
docker compose ps
- Check the browser console for errors with loading remote components
- Verify the Remote application is properly exposing components
- Check CORS headers and Nginx configuration
- Make sure the referer header is being properly passed
- Restart services:
docker compose restart nginx - Restart all containers:
docker compose down && docker compose up -d - Clear browser cache and cookies
Custom error pages are located in /nginx/error_pages/. You can modify these files to change the appearance and messaging of error pages.
To expose additional components from the Remote application:
- Add new components to
/remote/src/components/ - Update
/remote/rspack.config.jsto expose the components - Import them in the Host application
To modify authentication behavior:
- Update the Nginx configuration in
/nginx/default.conf - Adjust the Authentik Provider settings in the admin interface
- This setup prevents direct access to Remote components
- All requests to Remote are authenticated and verified
- User information is securely passed between applications
- Logout functionality is provided to end user sessions
- Custom error pages don't leak sensitive information
/nginx/default.conf- Nginx configuration with authentication and routing/host/src/App.js- Host application with Remote component imports/remote/src/components/RemoteComponent.js- Component exposed by Remote/docker-compose.yml- Service definitions and networking
- Host (Consumer):
/host/rspack.config.js - Remote (Provider):
/remote/rspack.config.js