Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

desktopAuth.ts

144 lines4.5 KB

1export type DesktopCloudAuthOAuthStrategy = `oauth_${string}`;
2​
3export interface DesktopCloudAuthOAuthOption {
4 readonly strategy: DesktopCloudAuthOAuthStrategy;
5 readonly label: string;
6 readonly providerId: string;
7 readonly iconUrl: string | null;
8}
9​
10interface ClerkOAuthProviderSetting {
11 readonly enabled?: unknown;
12 readonly authenticatable?: unknown;
13 readonly strategy?: unknown;
14 readonly name?: unknown;
15 readonly logo_url?: unknown;
17​
18interface ClerkUserSettingsLike {
19 readonly authenticatableSocialStrategies?: unknown;
20 readonly social?: unknown;
22​
23interface ClerkEnvironmentLike {
24 readonly userSettings?: ClerkUserSettingsLike;
26​
27interface ClerkLike {
28 readonly __internal_environment?: ClerkEnvironmentLike;
29 readonly environment?: ClerkEnvironmentLike;
31​
32const isClerkOAuthProviderSetting = (value: unknown): value is ClerkOAuthProviderSetting =>
33 typeof value === "object" && value !== null;
34​
35const OAUTH_LABELS: Readonly<Record<string, string>> = {
36 oauth_apple: "Apple",
37 oauth_discord: "Discord",
38 oauth_github: "GitHub",
39 oauth_gitlab: "GitLab",
40 oauth_google: "Google",
41 oauth_linear: "Linear",
42 oauth_microsoft: "Microsoft",
43 oauth_slack: "Slack",
44 oauth_x: "X",
45};
46​
47// Mirrors Clerk UI's enabled-provider projection for the local desktop replacement:
48// https://github.com/clerk/javascript/blob/52861184477bee99c71552000311a289e91d3b59/packages/ui/src/hooks/useEnabledThirdPartyProviders.tsx
49export function isDesktopCloudAuthOAuthStrategy(
50 value: unknown,
51): value is DesktopCloudAuthOAuthStrategy {
52 return typeof value === "string" && value.startsWith("oauth_");
54​
55export function getDesktopCloudAuthOAuthStrategyLabel(
56 strategy: DesktopCloudAuthOAuthStrategy,
57): string {
58 const mapped = OAUTH_LABELS[strategy];
59 if (mapped) return mapped;
60 return strategy
61 .replace(/^oauth_custom_/, "")
62 .replace(/^oauth_/, "")
63 .split("_")
64 .filter(Boolean)
65 .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
66 .join(" ");
68​
69export function resolveDesktopCloudAuthOAuthOptions(
70 clerk: unknown,
71): readonly DesktopCloudAuthOAuthOption[] {
72 const environment =
73 (clerk as ClerkLike | null | undefined)?.__internal_environment ??
74 (clerk as ClerkLike | null | undefined)?.environment;
75 const userSettings = environment?.userSettings;
76 const strategies = userSettings?.authenticatableSocialStrategies;
77 if (Array.isArray(strategies)) {
78 return uniqueOptions(
79 strategies
80 .filter(isDesktopCloudAuthOAuthStrategy)
81 .map((strategy) =>
82 createOAuthOption(strategy, findProviderSetting(userSettings, strategy)),
83 ),
84 );
85 }
86​
87 const social = userSettings?.social;
88 if (!social || typeof social !== "object") {
89 return [];
90 }
91​
92 return uniqueOptions(
93 Object.values(social as Record<string, ClerkOAuthProviderSetting>)
94 .filter(isClerkOAuthProviderSetting)
95 .filter((provider) => provider.enabled !== false && provider.authenticatable !== false)
96 .map((provider) => {
97 const strategy = isDesktopCloudAuthOAuthStrategy(provider.strategy)
98 ? provider.strategy
99 : null;
100 if (!strategy) return null;
101 return createOAuthOption(strategy, provider);
102 })
103 .filter((option): option is DesktopCloudAuthOAuthOption => option !== null),
104 );
106​
107function findProviderSetting(
108 userSettings: ClerkUserSettingsLike | undefined,
109 strategy: DesktopCloudAuthOAuthStrategy,
110): ClerkOAuthProviderSetting | undefined {
111 const social = userSettings?.social;
112 if (!social || typeof social !== "object") return undefined;
113​
114 return Object.values(social as Record<string, ClerkOAuthProviderSetting>)
115 .filter(isClerkOAuthProviderSetting)
116 .find((provider) => provider.strategy === strategy);
118​
119function createOAuthOption(
120 strategy: DesktopCloudAuthOAuthStrategy,
121 provider?: ClerkOAuthProviderSetting,
122): DesktopCloudAuthOAuthOption {
123 return {
124 strategy,
125 label:
126 typeof provider?.name === "string" && provider.name.trim()
127 ? provider.name
128 : getDesktopCloudAuthOAuthStrategyLabel(strategy),
129 providerId: strategy.replace(/^oauth_/, ""),
130 iconUrl:
131 typeof provider?.logo_url === "string" && provider.logo_url.trim() ? provider.logo_url : null,
132 };
134​
135function uniqueOptions(
136 options: readonly DesktopCloudAuthOAuthOption[],
137): readonly DesktopCloudAuthOAuthOption[] {
138 const seen = new Set<string>();
139 return options.filter((option) => {
140 if (seen.has(option.strategy)) return false;
141 seen.add(option.strategy);
142 return true;
143 });