auth

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

config.ts (1069B)


      1 interface AppUrls {
      2   basePath: string;
      3   appUrl: (path?: string) => string;
      4 }
      5 
      6 const authUrlPromises = new Map<string, Promise<string>>();
      7 
      8 export function createAppUrls(appPath: string): AppUrls {
      9   const current = window.location.pathname;
     10   const basePath =
     11     current === appPath || current.startsWith(`${appPath}/`) ? appPath : '';
     12   return {
     13     basePath,
     14     appUrl: (path = '') => `${window.location.origin}${basePath}${path}`,
     15   };
     16 }
     17 
     18 export function getAuthUrl(basePath: string) {
     19   let promise = authUrlPromises.get(basePath);
     20   if (!promise) {
     21     promise = (async () => {
     22       try {
     23         const response = await fetch(`${basePath}/config.json`, {
     24           credentials: 'same-origin',
     25         });
     26         if (response.ok) {
     27           const value = await response.json();
     28           if (typeof value?.auth_url === 'string' && value.auth_url) {
     29             return value.auth_url.replace(/\/+$/, '');
     30           }
     31         }
     32       } catch {}
     33       return window.location.origin;
     34     })();
     35     authUrlPromises.set(basePath, promise);
     36   }
     37   return promise;
     38 }