File size: 1,519 Bytes
6678fa1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
export { COOKIE_NAME, ONE_YEAR_MS } from "@shared/const";

// Auth configuration type
export type AuthConfig = {
  hfOAuthEnabled: boolean;
  devLoginEnabled: boolean;
};

// Fetch auth config from server
let cachedAuthConfig: AuthConfig | null = null;
export const getAuthConfig = async (): Promise<AuthConfig> => {
  if (cachedAuthConfig) return cachedAuthConfig;
  try {
    const response = await fetch("/api/auth/config");
    cachedAuthConfig = await response.json();
    return cachedAuthConfig!;
  } catch {
    return { hfOAuthEnabled: false, devLoginEnabled: true };
  }
};

// Get login URL based on available auth methods
export const getLoginUrl = (): string => {
  const oauthPortalUrl = import.meta.env.VITE_OAUTH_PORTAL_URL;
  const appId = import.meta.env.VITE_APP_ID;
  
  // If standard OAuth is configured, use it
  if (oauthPortalUrl && appId) {
  const redirectUri = `${window.location.origin}/api/oauth/callback`;
  const state = btoa(redirectUri);

  const url = new URL(`${oauthPortalUrl}/app-auth`);
  url.searchParams.set("appId", appId);
  url.searchParams.set("redirectUri", redirectUri);
  url.searchParams.set("state", state);
  url.searchParams.set("type", "signIn");

  return url.toString();
  }
  
  // Return placeholder - frontend will check auth config for alternatives
  return "#oauth-not-configured";
};

// Get HuggingFace login URL
export const getHfLoginUrl = (): string => "/api/hf/login";

// Get dev login URL
export const getDevLoginUrl = (): string => "/api/dev/login";