Implemented partnerui
This commit is contained in:
@@ -11,36 +11,86 @@ export const LOCAL_STORAGE_ACTIVATION_REASON_KEY = 'pfm_user_pending_activation_
|
||||
export function isUserSyncEndpoint(url?: string): boolean {
|
||||
if (!url) return false;
|
||||
const clean = url.toLowerCase().split('?')[0];
|
||||
return clean.includes('/users/sync') || clean.includes('/integration/sync') || clean.includes('/user/sync');
|
||||
return (
|
||||
clean.includes('/users/sync') ||
|
||||
clean.includes('/integration/sync') ||
|
||||
clean.includes('/user/sync') ||
|
||||
clean.endsWith('/sync') ||
|
||||
clean === '/sync'
|
||||
);
|
||||
}
|
||||
|
||||
export function isTenantEndpoint(url?: string): boolean {
|
||||
if (!url) return false;
|
||||
return url.toLowerCase().includes('/governance/tenants');
|
||||
const clean = url.toLowerCase().split('?')[0];
|
||||
return clean.includes('/governance/tenants');
|
||||
}
|
||||
|
||||
type SessionExpiredListener = (reason?: string) => void;
|
||||
|
||||
const listeners = new Set<SessionExpiredListener>();
|
||||
let hasTriggeredSessionExpired = false;
|
||||
|
||||
export function dismissSubordinateModals(): void {
|
||||
if (typeof window !== 'undefined') {
|
||||
window.dispatchEvent(new CustomEvent('pfm:dismiss-subordinates'));
|
||||
}
|
||||
}
|
||||
|
||||
export function cancelSessionExpiration(): void {
|
||||
hasTriggeredSessionExpired = false;
|
||||
if (typeof window !== 'undefined') {
|
||||
window.dispatchEvent(new CustomEvent('pfm:session-expired-cancelled'));
|
||||
}
|
||||
}
|
||||
|
||||
export function isSessionExpiredState(): boolean {
|
||||
return hasTriggeredSessionExpired;
|
||||
}
|
||||
|
||||
export function triggerSessionExpired(reason?: string, requestUrl?: string): void {
|
||||
if (requestUrl && isUserSyncEndpoint(requestUrl)) return;
|
||||
if (typeof window !== 'undefined' && localStorage.getItem(LOCAL_STORAGE_ACTIVATION_PENDING_KEY)) return;
|
||||
if (hasTriggeredSessionExpired) return;
|
||||
if (requestUrl && isUserSyncEndpoint(requestUrl)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof window !== 'undefined' && localStorage.getItem(LOCAL_STORAGE_ACTIVATION_PENDING_KEY)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (hasTriggeredSessionExpired) {
|
||||
return;
|
||||
}
|
||||
|
||||
const hasExistingToken = typeof window !== 'undefined' && !!localStorage.getItem(LOCAL_STORAGE_TOKEN_KEY);
|
||||
if (!hasExistingToken) return;
|
||||
if (!hasExistingToken) {
|
||||
return;
|
||||
}
|
||||
|
||||
hasTriggeredSessionExpired = true;
|
||||
dismissSubordinateModals();
|
||||
|
||||
if (typeof window !== 'undefined') {
|
||||
window.dispatchEvent(new CustomEvent('pfm:session-expired', { detail: { reason: reason || 'HTTP 401 Unauthorized' } }));
|
||||
window.dispatchEvent(
|
||||
new CustomEvent('pfm:session-expired', {
|
||||
detail: { reason: reason || 'HTTP 401 Unauthorized' },
|
||||
})
|
||||
);
|
||||
}
|
||||
listeners.forEach((cb) => cb(reason));
|
||||
|
||||
listeners.forEach((callback) => {
|
||||
try {
|
||||
callback(reason);
|
||||
} catch (err) {
|
||||
console.warn('[SessionExpiration] Callback execution error:', err);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function onSessionExpired(listener: SessionExpiredListener): () => void {
|
||||
listeners.add(listener);
|
||||
return () => { listeners.delete(listener); };
|
||||
return () => {
|
||||
listeners.delete(listener);
|
||||
};
|
||||
}
|
||||
|
||||
export function resetSessionExpiredState(): void {
|
||||
@@ -49,6 +99,7 @@ export function resetSessionExpiredState(): void {
|
||||
|
||||
export function clearSessionAndRedirectToLanding(): void {
|
||||
if (typeof window === 'undefined') return;
|
||||
|
||||
try {
|
||||
localStorage.removeItem(LOCAL_STORAGE_TOKEN_KEY);
|
||||
localStorage.removeItem(LOCAL_STORAGE_USER_KEY);
|
||||
@@ -57,8 +108,16 @@ export function clearSessionAndRedirectToLanding(): void {
|
||||
localStorage.removeItem(LOCAL_STORAGE_USER_SYNC_KEY);
|
||||
localStorage.removeItem(LOCAL_STORAGE_ACTIVATION_PENDING_KEY);
|
||||
localStorage.removeItem(LOCAL_STORAGE_ACTIVATION_REASON_KEY);
|
||||
} catch {}
|
||||
try { queryClient.clear(); } catch {}
|
||||
} catch (err) {
|
||||
console.warn('[SessionExpiration] Storage cleanup warning:', err);
|
||||
}
|
||||
|
||||
try {
|
||||
queryClient.clear();
|
||||
} catch (err) {
|
||||
console.warn('[SessionExpiration] QueryClient clear warning:', err);
|
||||
}
|
||||
|
||||
resetSessionExpiredState();
|
||||
window.location.replace('/');
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user