codalware-auth
Version:
Complete authentication system with enterprise security, attack protection, team workspaces, waitlist, billing, UI components, 2FA, and account recovery - production-ready in 5 minutes. Enhanced CLI with verification, rollback, and App Router scaffolding.
47 lines (42 loc) • 1.51 kB
text/typescript
// Lightweight router-agnostic navigation helpers.
// These avoid depending on Next's pages-router hooks so components work in App Router and Pages Router apps.
export const navigate = (href: string, opts?: { replace?: boolean }) => {
if (typeof window === 'undefined') return;
const { replace = false } = opts || {};
try {
if (replace) {
window.location.replace(href);
} else {
// use assign to preserve behavior similar to router.push
window.location.assign(href);
}
} catch {
// fallback
if (replace) window.location.href = href;
else window.location.href = href;
}
};
export const goBack = () => {
if (typeof window === 'undefined') return;
window.history.back();
};
export const getQueryParam = (name: string): string | null => {
if (typeof window === 'undefined') return null;
try {
const params = new URLSearchParams(window.location.search);
return params.get(name);
} catch {
return null;
}
};
export const getPathname = (): string => {
if (typeof window === 'undefined') return '';
return window.location.pathname || '';
};
// A small helper that subscribes to popstate for client-side updates (consumers can use it if needed)
export const onPathChange = (cb: () => void) => {
if (typeof window === 'undefined') return () => {};
const handler = () => cb();
window.addEventListener('popstate', handler);
return () => window.removeEventListener('popstate', handler);
};