auth

Main authentication module for my projects
git clone git://git.finwo.net/app/auth
Log | Files | Refs

router.ts (579B)


      1 export function currentRoute() {
      2   const value = location.hash.replace(/^#/, '');
      3   return value.startsWith('/') ? value : '/account/overview';
      4 }
      5 
      6 export function navigate(path: string) {
      7   if (!path) return;
      8   if (currentRoute() === path) {
      9     window.dispatchEvent(new HashChangeEvent('hashchange'));
     10     return;
     11   }
     12   location.hash = path;
     13 }
     14 
     15 export function createRouter(onChange: (path: string) => void) {
     16   const handler = () => onChange(currentRoute());
     17   window.addEventListener('hashchange', handler);
     18   return () => window.removeEventListener('hashchange', handler);
     19 }