feat: Refine tenant route matching and implement role-based login redirection for super admins and tenant users.

This commit is contained in:
Yashwin 2026-03-23 12:01:20 +05:30
parent 20d802555e
commit 93ad8feea9
4 changed files with 22 additions and 9 deletions

View File

@ -70,8 +70,12 @@ export const Header = ({ breadcrumbs, currentPage, onMenuClick }: HeaderProps):
setIsDropdownOpen(false); setIsDropdownOpen(false);
// Check if user is on a tenant route to determine redirect path // Check if user is on a tenant route to determine redirect path
const isTenantRoute = window.location.pathname.startsWith('/tenant'); // Note: use /tenant/ instead of /tenant to avoid matching /tenants
const redirectPath = isTenantRoute ? '/tenant/login' : '/'; const isTenantRoute = window.location.pathname.startsWith('/tenant/') || window.location.pathname === '/tenant';
const isSuperAdmin = roles.includes('super_admin');
// Super admins always go to root login, tenant users go to /tenant/login if on a tenant route
const redirectPath = isSuperAdmin ? '/' : (isTenantRoute ? '/tenant/login' : '/');
try { try {
// Call logout API with Bearer token // Call logout API with Bearer token

View File

@ -57,7 +57,8 @@ export const PrimaryButton = ({
const isTenantAdmin = !isSuperAdmin && rolesArray.length > 0; const isTenantAdmin = !isSuperAdmin && rolesArray.length > 0;
// Check if we're on a tenant route (for login page where user might not be authenticated) // Check if we're on a tenant route (for login page where user might not be authenticated)
const isTenantRoute = typeof window !== 'undefined' && window.location.pathname.startsWith('/tenant'); // Use /tenant/ or exactly /tenant to avoid matching /tenants page
const isTenantRoute = typeof window !== 'undefined' && (window.location.pathname.startsWith('/tenant/') || window.location.pathname === '/tenant');
// Use theme colors for tenant admin or tenant routes, default colors for super admin // Use theme colors for tenant admin or tenant routes, default colors for super admin
const shouldUseTheme = (isTenantAdmin || (isTenantRoute && !isSuperAdmin)) && theme; const shouldUseTheme = (isTenantAdmin || (isTenantRoute && !isSuperAdmin)) && theme;

View File

@ -50,7 +50,8 @@ export const SecondaryButton = ({
const isTenantAdmin = !isSuperAdmin && rolesArray.length > 0; const isTenantAdmin = !isSuperAdmin && rolesArray.length > 0;
// Check if we're on a tenant route (for login page where user might not be authenticated) // Check if we're on a tenant route (for login page where user might not be authenticated)
const isTenantRoute = typeof window !== 'undefined' && window.location.pathname.startsWith('/tenant'); // Use /tenant/ or exactly /tenant to avoid matching /tenants page
const isTenantRoute = typeof window !== 'undefined' && (window.location.pathname.startsWith('/tenant/') || window.location.pathname === '/tenant');
// Use theme colors for tenant admin or tenant routes, default colors for super admin // Use theme colors for tenant admin or tenant routes, default colors for super admin
const shouldUseTheme = (isTenantAdmin || (isTenantRoute && !isSuperAdmin)) && theme; const shouldUseTheme = (isTenantAdmin || (isTenantRoute && !isSuperAdmin)) && theme;

View File

@ -57,19 +57,26 @@ apiClient.interceptors.response.use(
if (!isAuthEndpoint) { if (!isAuthEndpoint) {
// Handle unauthorized - clear auth and redirect to login (only for non-auth endpoints) // Handle unauthorized - clear auth and redirect to login (only for non-auth endpoints)
// Check if user is on a tenant route to determine redirect path // Check if user is on a tenant route to determine redirect path
const isTenantRoute = window.location.pathname.startsWith('/tenant'); const isTenantRoute = window.location.pathname.startsWith('/tenant/') || window.location.pathname === '/tenant';
const redirectPath = isTenantRoute ? '/tenant/login' : '/';
let redirectPath = '/';
try { try {
const store = (window as any).__REDUX_STORE__; const store = (window as any).__REDUX_STORE__;
if (store) { if (store) {
const state = store.getState() as RootState;
const isSuperAdmin = state.auth.roles.includes('super_admin');
// Super admins always go to root login, tenant users go to /tenant/login if on a tenant route
redirectPath = isSuperAdmin ? '/' : (isTenantRoute ? '/tenant/login' : '/');
store.dispatch({ type: 'auth/logout' }); store.dispatch({ type: 'auth/logout' });
navigate(redirectPath, { replace: true });
} }
} catch (e) { } catch (e) {
// Silently fail if store is not available // Fallback if store is not available
navigate(redirectPath, { replace: true }); redirectPath = isTenantRoute ? '/tenant/login' : '/';
} }
navigate(redirectPath, { replace: true });
} }
// For auth endpoints, just reject the promise so the component can handle the error // For auth endpoints, just reject the promise so the component can handle the error
} }