This repository implements a Modern Redirect-Based Single Sign-On (SSO) Architecture tailored for distributed microservice ecosystems. It demonstrates the exact federated authentication patterns used in production platforms (such as Mandi / Trucking platforms), featuring centralized login UI redirection, secure JWT verification, and loop-free account switching.
The system consists of two autonomous Node.js / Express microservices:
-
App 1: Central Identity Provider (IdP) β Port 5001
- Serves as the central user identity registry (backed by MongoDB & bcrypt).
- Hosts the unified SSO Login and Registration user interfaces.
- Handles
returnUrlredirections andclearCache=truesession purges. - Exposes a secure verification endpoint (
GET /auth/me) for connected Service Providers.
-
App 2: Service Provider (SP) β Port 5002
- Represents a connected domain application (e.g., Fleet Dispatches & Logistics).
- Automatically detects unauthenticated access and initiates OAuth-style redirects to App 1.
- Extracts signed JWT tokens upon return redirection (
/auth-callback?token=...) and validates them directly against App 1's server. - Implements robust permission checking with a fallback Switch Account workflow.
- User navigates to App 2 (
http://localhost:5002). - App 2 detects an unauthenticated session and redirects the user to:
http://localhost:5001/login?returnUrl=http://localhost:5002/auth-callback - If the user is already authenticated in App 1 (active IdP session), App 1 immediately redirects back with the token.
- If unauthenticated, the user logs in on App 1. Upon verification, App 1 redirects to:
http://localhost:5002/auth-callback?token=<signed_jwt> - App 2 verifies the JWT via App 1's backend API (
/auth/me), establishes a localized Service Provider session inlocalStorage, and displays the protected dashboard!
A common pitfall in federated SSO systems is an infinite redirect loop when an authenticated IdP user lacks authorization for a Service Provider app. This project resolves this completely:
- When a user signs in with an unauthorized account (demonstrated by registering an email ending in
@guest.com), App 2 intercepts the authentication failure and presents an Access Restricted card. - Clicking Switch Account redirects the user to App 1 with:
http://localhost:5001/login?clearCache=true&returnUrl=http://localhost:5002/auth-callback - App 1 intercepts
clearCache=true, purges all existing local session storage instantly without flashing or spinning, and displays a clean login prompt ready for an authorized account!
- Open PowerShell in the project root directory (
C:\Users\Admin\Desktop\Voyage\SSO). - Run the automated start script:
.\start-all.ps1 - This script will install dependencies and boot both microservices simultaneously:
- App 1 (IdP):
http://localhost:5001 - App 2 (SP):
http://localhost:5002
- App 1 (IdP):
- Open
index.htmlin any web browser to view the interactive Architecture Portal and test the flows!
- Authorized Access: Log in or sign up with any standard email (e.g.,
user@voyage.com). Navigate between App 1 and App 2 without re-entering credentials. - Unauthorized Demo (Switch Account): Register a new account ending in
@guest.com(e.g.,test@guest.com). Try launching App 2 to test the graceful access restriction and seamless session reset!