@lukoweb/apitogo 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli/cli.js +168 -57
- package/dist/declarations/lib/ui/Command.d.ts +1 -1
- package/dist/declarations/lib/util/MdxComponents.d.ts +1 -1
- package/package.json +2 -1
- package/src/app/main.tsx +150 -150
- package/src/config/loader.ts +245 -245
- package/src/index.ts +33 -33
- package/src/lib/authentication/components/CallbackHandler.tsx +45 -45
- package/src/lib/authentication/components/SignIn.tsx +46 -46
- package/src/lib/authentication/components/SignUp.tsx +48 -48
- package/src/lib/authentication/providers/clerk.tsx +224 -224
- package/src/lib/authentication/ui/AuthCard.tsx +7 -7
- package/src/lib/authentication/ui/EmailLinkCallbackUi.tsx +129 -125
- package/src/lib/authentication/ui/EmailLinkSentUi.tsx +105 -101
- package/src/lib/authentication/ui/EmailLinkSignInUi.tsx +100 -96
- package/src/lib/authentication/ui/EmailVerificationUi.tsx +128 -124
- package/src/lib/authentication/ui/ZudokuAuthUi.tsx +620 -616
- package/src/lib/components/AiAssistantMenuItems.tsx +102 -102
- package/src/lib/components/Autocomplete.tsx +123 -123
- package/src/lib/components/DeveloperHint.tsx +39 -35
- package/src/lib/components/Header.tsx +260 -260
- package/src/lib/components/Layout.tsx +53 -53
- package/src/lib/components/Main.tsx +51 -51
- package/src/lib/components/Mermaid.tsx +74 -70
- package/src/lib/components/MobileTopNavigation.tsx +276 -276
- package/src/lib/components/MultiSelect.tsx +86 -82
- package/src/lib/components/TopNavigation.tsx +103 -103
- package/src/lib/components/index.ts +26 -26
- package/src/lib/components/navigation/NavigationCategory.tsx +157 -157
- package/src/lib/components/navigation/NavigationFilterInput.tsx +35 -35
- package/src/lib/components/navigation/NavigationItem.tsx +181 -177
- package/src/lib/core/RouteGuard.tsx +193 -193
- package/src/lib/errors/ErrorMessage.tsx +50 -46
- package/src/lib/plugins/api-catalog/Catalog.tsx +80 -80
- package/src/lib/plugins/api-keys/CreateApiKey.tsx +128 -124
- package/src/lib/plugins/api-keys/CreateApiKeyDialog.tsx +49 -49
- package/src/lib/plugins/api-keys/SettingsApiKeys.tsx +72 -72
- package/src/lib/plugins/api-keys/settings/ApiKeyItem.tsx +342 -342
- package/src/lib/plugins/api-keys/settings/RevealApiKey.tsx +127 -127
- package/src/lib/plugins/markdown/MdxPage.tsx +285 -285
- package/src/lib/plugins/openapi/ApiHeader.tsx +96 -96
- package/src/lib/plugins/openapi/CollapsibleCode.tsx +88 -88
- package/src/lib/plugins/openapi/DownloadSchemaButton.tsx +94 -94
- package/src/lib/plugins/openapi/GeneratedExampleSidecarBox.tsx +52 -52
- package/src/lib/plugins/openapi/OperationListItem.tsx +183 -183
- package/src/lib/plugins/openapi/RequestBodySidecarBox.tsx +63 -63
- package/src/lib/plugins/openapi/ResponsesSidecarBox.tsx +120 -117
- package/src/lib/plugins/openapi/SchemaInfo.tsx +344 -344
- package/src/lib/plugins/openapi/SchemaList.tsx +113 -113
- package/src/lib/plugins/openapi/Sidecar.tsx +312 -309
- package/src/lib/plugins/openapi/SidecarExamples.tsx +177 -174
- package/src/lib/plugins/openapi/components/EnumValues.tsx +58 -58
- package/src/lib/plugins/openapi/components/ResponseContent.tsx +117 -114
- package/src/lib/plugins/openapi/playground/BodyPanel.tsx +274 -271
- package/src/lib/plugins/openapi/playground/CollapsibleHeader.tsx +51 -51
- package/src/lib/plugins/openapi/playground/ExamplesDropdown.tsx +60 -60
- package/src/lib/plugins/openapi/playground/Headers.tsx +188 -181
- package/src/lib/plugins/openapi/playground/IdentityDialog.tsx +75 -75
- package/src/lib/plugins/openapi/playground/IdentitySelector.tsx +38 -38
- package/src/lib/plugins/openapi/playground/ParamsGrid.tsx +45 -45
- package/src/lib/plugins/openapi/playground/Playground.tsx +602 -599
- package/src/lib/plugins/openapi/playground/PlaygroundDialog.tsx +56 -56
- package/src/lib/plugins/openapi/playground/QueryParams.tsx +151 -148
- package/src/lib/plugins/openapi/playground/RequestLoginDialog.tsx +70 -70
- package/src/lib/plugins/openapi/playground/request-panel/MultipartField.tsx +91 -91
- package/src/lib/plugins/openapi/playground/result-panel/AudioPlayer.tsx +50 -50
- package/src/lib/plugins/openapi/playground/result-panel/ResponseTab.tsx +355 -355
- package/src/lib/plugins/openapi/playground/result-panel/ResultPanel.tsx +106 -102
- package/src/lib/plugins/openapi/schema/SchemaPropertyItem.tsx +189 -184
- package/src/lib/plugins/openapi/schema/SchemaView.tsx +299 -299
- package/src/lib/plugins/search-pagefind/IndexingDialog.tsx +162 -162
- package/src/lib/plugins/search-pagefind/PagefindSearch.tsx +202 -202
- package/src/lib/plugins/search-pagefind/ResultList.tsx +122 -118
- package/src/lib/ui/ActionButton.tsx +21 -21
- package/src/lib/ui/Command.tsx +191 -191
- package/src/lib/util/MdxComponents.tsx +150 -150
- package/src/vite/config.ts +234 -231
- package/src/vite/dev-server.ts +291 -291
- package/src/vite/plugin-api-keys.ts +50 -50
- package/src/vite/plugin-api.ts +320 -318
- package/src/vite/plugin-auth.ts +38 -38
- package/src/vite/plugin-component.ts +45 -39
- package/src/vite/plugin-config.ts +75 -75
- package/src/vite/plugin-custom-pages.ts +36 -36
- package/src/vite/plugin-docs.ts +202 -202
- package/src/vite/plugin-markdown-export.ts +214 -214
- package/src/vite/plugin-mdx.ts +149 -149
- package/src/vite/plugin-navigation.ts +106 -106
- package/src/vite/plugin-search.ts +47 -47
- package/src/vite/plugin.ts +42 -42
- package/src/vite/prerender/prerender.ts +233 -233
|
@@ -1,46 +1,46 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
} from "
|
|
11
|
-
import { useLatest } from "../../util/useLatest.js";
|
|
12
|
-
import { useAuth } from "../hook.js";
|
|
13
|
-
|
|
14
|
-
export const SignIn = () => {
|
|
15
|
-
const auth = useAuth();
|
|
16
|
-
const [search] = useSearchParams();
|
|
17
|
-
const redirectTo = search.get("redirect") ?? undefined;
|
|
18
|
-
|
|
19
|
-
const login = useLatest(auth.login);
|
|
20
|
-
|
|
21
|
-
useEffect(() => {
|
|
22
|
-
void login.current({
|
|
23
|
-
redirectTo: redirectTo ?? "/",
|
|
24
|
-
replace: true,
|
|
25
|
-
});
|
|
26
|
-
}, [login, redirectTo]);
|
|
27
|
-
|
|
28
|
-
return (
|
|
29
|
-
<div className="flex items-center justify-center mt-8">
|
|
30
|
-
<Card className="max-w-md w-full">
|
|
31
|
-
<CardHeader>
|
|
32
|
-
<CardTitle className="text-lg">Sign in</CardTitle>
|
|
33
|
-
<CardDescription>
|
|
34
|
-
You're being redirected to our secure login provider to complete
|
|
35
|
-
your sign-in process.
|
|
36
|
-
</CardDescription>
|
|
37
|
-
</CardHeader>
|
|
38
|
-
<CardContent>
|
|
39
|
-
<div className="flex items-center text-sm font-medium gap-2">
|
|
40
|
-
<Spinner /> Redirecting...
|
|
41
|
-
</div>
|
|
42
|
-
</CardContent>
|
|
43
|
-
</Card>
|
|
44
|
-
</div>
|
|
45
|
-
);
|
|
46
|
-
};
|
|
1
|
+
import { Spinner } from "@lukoweb/apitogo/components";
|
|
2
|
+
import {
|
|
3
|
+
Card,
|
|
4
|
+
CardContent,
|
|
5
|
+
CardDescription,
|
|
6
|
+
CardHeader,
|
|
7
|
+
CardTitle,
|
|
8
|
+
} from "@lukoweb/apitogo/ui/Card.js";
|
|
9
|
+
import { useEffect } from "react";
|
|
10
|
+
import { useSearchParams } from "react-router";
|
|
11
|
+
import { useLatest } from "../../util/useLatest.js";
|
|
12
|
+
import { useAuth } from "../hook.js";
|
|
13
|
+
|
|
14
|
+
export const SignIn = () => {
|
|
15
|
+
const auth = useAuth();
|
|
16
|
+
const [search] = useSearchParams();
|
|
17
|
+
const redirectTo = search.get("redirect") ?? undefined;
|
|
18
|
+
|
|
19
|
+
const login = useLatest(auth.login);
|
|
20
|
+
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
void login.current({
|
|
23
|
+
redirectTo: redirectTo ?? "/",
|
|
24
|
+
replace: true,
|
|
25
|
+
});
|
|
26
|
+
}, [login, redirectTo]);
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<div className="flex items-center justify-center mt-8">
|
|
30
|
+
<Card className="max-w-md w-full">
|
|
31
|
+
<CardHeader>
|
|
32
|
+
<CardTitle className="text-lg">Sign in</CardTitle>
|
|
33
|
+
<CardDescription>
|
|
34
|
+
You're being redirected to our secure login provider to complete
|
|
35
|
+
your sign-in process.
|
|
36
|
+
</CardDescription>
|
|
37
|
+
</CardHeader>
|
|
38
|
+
<CardContent>
|
|
39
|
+
<div className="flex items-center text-sm font-medium gap-2">
|
|
40
|
+
<Spinner /> Redirecting...
|
|
41
|
+
</div>
|
|
42
|
+
</CardContent>
|
|
43
|
+
</Card>
|
|
44
|
+
</div>
|
|
45
|
+
);
|
|
46
|
+
};
|
|
@@ -1,48 +1,48 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
} from "
|
|
11
|
-
import { useLatest } from "../../util/useLatest.js";
|
|
12
|
-
import { useAuth } from "../hook.js";
|
|
13
|
-
|
|
14
|
-
export const SignUp = () => {
|
|
15
|
-
const auth = useAuth();
|
|
16
|
-
const [search] = useSearchParams();
|
|
17
|
-
const redirectTo = search.get("redirect") ?? "/";
|
|
18
|
-
|
|
19
|
-
const signup = useLatest(auth.signup);
|
|
20
|
-
|
|
21
|
-
useEffect(() => {
|
|
22
|
-
void signup.current({ redirectTo });
|
|
23
|
-
}, [signup, redirectTo]);
|
|
24
|
-
|
|
25
|
-
return (
|
|
26
|
-
<div className="flex items-center justify-center mt-8">
|
|
27
|
-
<Card className="max-w-md w-full">
|
|
28
|
-
<CardHeader>
|
|
29
|
-
<CardTitle className="text-lg">Sign up</CardTitle>
|
|
30
|
-
<CardDescription>
|
|
31
|
-
You're being redirected to our secure login provider to complete
|
|
32
|
-
your sign up process.
|
|
33
|
-
</CardDescription>
|
|
34
|
-
</CardHeader>
|
|
35
|
-
<CardContent>
|
|
36
|
-
<div className="flex flex-col gap-2 justify-center">
|
|
37
|
-
<Button onClick={() => auth.signup()} variant="default">
|
|
38
|
-
Register
|
|
39
|
-
</Button>
|
|
40
|
-
<Button variant="link" className="text-muted-foreground" asChild>
|
|
41
|
-
<Link to="/">Go home</Link>
|
|
42
|
-
</Button>
|
|
43
|
-
</div>
|
|
44
|
-
</CardContent>
|
|
45
|
-
</Card>
|
|
46
|
-
</div>
|
|
47
|
-
);
|
|
48
|
-
};
|
|
1
|
+
import { Button, Link } from "@lukoweb/apitogo/components";
|
|
2
|
+
import {
|
|
3
|
+
Card,
|
|
4
|
+
CardContent,
|
|
5
|
+
CardDescription,
|
|
6
|
+
CardHeader,
|
|
7
|
+
CardTitle,
|
|
8
|
+
} from "@lukoweb/apitogo/ui/Card.js";
|
|
9
|
+
import { useEffect } from "react";
|
|
10
|
+
import { useSearchParams } from "react-router";
|
|
11
|
+
import { useLatest } from "../../util/useLatest.js";
|
|
12
|
+
import { useAuth } from "../hook.js";
|
|
13
|
+
|
|
14
|
+
export const SignUp = () => {
|
|
15
|
+
const auth = useAuth();
|
|
16
|
+
const [search] = useSearchParams();
|
|
17
|
+
const redirectTo = search.get("redirect") ?? "/";
|
|
18
|
+
|
|
19
|
+
const signup = useLatest(auth.signup);
|
|
20
|
+
|
|
21
|
+
useEffect(() => {
|
|
22
|
+
void signup.current({ redirectTo });
|
|
23
|
+
}, [signup, redirectTo]);
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<div className="flex items-center justify-center mt-8">
|
|
27
|
+
<Card className="max-w-md w-full">
|
|
28
|
+
<CardHeader>
|
|
29
|
+
<CardTitle className="text-lg">Sign up</CardTitle>
|
|
30
|
+
<CardDescription>
|
|
31
|
+
You're being redirected to our secure login provider to complete
|
|
32
|
+
your sign up process.
|
|
33
|
+
</CardDescription>
|
|
34
|
+
</CardHeader>
|
|
35
|
+
<CardContent>
|
|
36
|
+
<div className="flex flex-col gap-2 justify-center">
|
|
37
|
+
<Button onClick={() => auth.signup()} variant="default">
|
|
38
|
+
Register
|
|
39
|
+
</Button>
|
|
40
|
+
<Button variant="link" className="text-muted-foreground" asChild>
|
|
41
|
+
<Link to="/">Go home</Link>
|
|
42
|
+
</Button>
|
|
43
|
+
</div>
|
|
44
|
+
</CardContent>
|
|
45
|
+
</Card>
|
|
46
|
+
</div>
|
|
47
|
+
);
|
|
48
|
+
};
|
|
@@ -1,224 +1,224 @@
|
|
|
1
|
-
import type { Clerk } from "@clerk/clerk-js";
|
|
2
|
-
import type { ZudokuPlugin } from "@lukoweb/apitogo/plugins";
|
|
3
|
-
import type { ClerkAuthenticationConfig } from "../../../config/config.js";
|
|
4
|
-
import type {
|
|
5
|
-
AuthActionContext,
|
|
6
|
-
AuthenticationPlugin,
|
|
7
|
-
AuthenticationProviderInitializer,
|
|
8
|
-
} from "../authentication.js";
|
|
9
|
-
import { SignIn } from "../components/SignIn.js";
|
|
10
|
-
import { SignOut } from "../components/SignOut.js";
|
|
11
|
-
import { SignUp } from "../components/SignUp.js";
|
|
12
|
-
import { type UserProfile, useAuthState } from "../state.js";
|
|
13
|
-
import { getClerkFrontendApi } from "./util.js";
|
|
14
|
-
|
|
15
|
-
export type ClerkProviderData = {
|
|
16
|
-
type: "clerk";
|
|
17
|
-
user: NonNullable<Clerk["session"]>["user"] | undefined;
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
declare module "../state.js" {
|
|
21
|
-
interface ProviderDataRegistry {
|
|
22
|
-
clerk: ClerkProviderData;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
let clerkPromise: Promise<Clerk> | undefined;
|
|
27
|
-
|
|
28
|
-
const loadClerk = (publishableKey: string): Promise<Clerk> => {
|
|
29
|
-
if (clerkPromise) return clerkPromise;
|
|
30
|
-
|
|
31
|
-
clerkPromise = new Promise<void>((resolve, reject) => {
|
|
32
|
-
const frontendApiUrl = getClerkFrontendApi(publishableKey);
|
|
33
|
-
|
|
34
|
-
const script = document.createElement("script");
|
|
35
|
-
script.src = `https://${frontendApiUrl}/npm/@clerk/clerk-js@5/dist/clerk.browser.js`;
|
|
36
|
-
script.async = true;
|
|
37
|
-
script.crossOrigin = "anonymous";
|
|
38
|
-
script.dataset.clerkPublishableKey = publishableKey;
|
|
39
|
-
script.onload = () => resolve();
|
|
40
|
-
script.onerror = () => {
|
|
41
|
-
clerkPromise = undefined;
|
|
42
|
-
reject(new Error("Failed to load Clerk from CDN"));
|
|
43
|
-
};
|
|
44
|
-
document.head.appendChild(script);
|
|
45
|
-
}).then(async () => {
|
|
46
|
-
const clerk = (window as { Clerk?: Clerk }).Clerk;
|
|
47
|
-
if (!clerk) {
|
|
48
|
-
throw new Error("Clerk script loaded but window.Clerk is not available");
|
|
49
|
-
}
|
|
50
|
-
await clerk.load();
|
|
51
|
-
return clerk;
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
return clerkPromise;
|
|
55
|
-
};
|
|
56
|
-
|
|
57
|
-
const clerkAuth: AuthenticationProviderInitializer<
|
|
58
|
-
ClerkAuthenticationConfig
|
|
59
|
-
> = ({
|
|
60
|
-
clerkPubKey,
|
|
61
|
-
jwtTemplateName,
|
|
62
|
-
redirectToAfterSignOut = "/",
|
|
63
|
-
redirectToAfterSignUp,
|
|
64
|
-
redirectToAfterSignIn,
|
|
65
|
-
}): AuthenticationPlugin & ZudokuPlugin => {
|
|
66
|
-
const getClerk = (): Promise<Clerk> => {
|
|
67
|
-
if (typeof window === "undefined") {
|
|
68
|
-
return Promise.reject(new Error("Clerk is not available during SSR"));
|
|
69
|
-
}
|
|
70
|
-
return loadClerk(clerkPubKey);
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
async function getAccessToken() {
|
|
74
|
-
const clerk = await getClerk();
|
|
75
|
-
|
|
76
|
-
if (!clerk.session) {
|
|
77
|
-
throw new Error("No session available");
|
|
78
|
-
}
|
|
79
|
-
const response = await clerk.session.getToken({
|
|
80
|
-
template: jwtTemplateName,
|
|
81
|
-
});
|
|
82
|
-
if (!response) {
|
|
83
|
-
throw new Error("Could not get access token from Clerk");
|
|
84
|
-
}
|
|
85
|
-
return response;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
async function getUserProfile(
|
|
89
|
-
clerk: Clerk,
|
|
90
|
-
): Promise<UserProfile | undefined> {
|
|
91
|
-
const user = clerk.session?.user;
|
|
92
|
-
if (!user) return undefined;
|
|
93
|
-
|
|
94
|
-
const verifiedEmail = user.emailAddresses.find(
|
|
95
|
-
(email) => email.verification.status === "verified",
|
|
96
|
-
);
|
|
97
|
-
|
|
98
|
-
return {
|
|
99
|
-
sub: user.id ?? "",
|
|
100
|
-
email:
|
|
101
|
-
verifiedEmail?.emailAddress ??
|
|
102
|
-
user.emailAddresses[0]?.emailAddress ??
|
|
103
|
-
"",
|
|
104
|
-
name: user.fullName ?? "",
|
|
105
|
-
emailVerified: !!verifiedEmail?.emailAddress,
|
|
106
|
-
pictureUrl: user.imageUrl ?? "",
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
async function refreshUserProfile() {
|
|
111
|
-
const clerk = await getClerk().catch((e) => {
|
|
112
|
-
// biome-ignore lint/suspicious/noConsole: Intentional warning
|
|
113
|
-
console.warn("Clerk unavailable during profile refresh:", e);
|
|
114
|
-
return undefined;
|
|
115
|
-
});
|
|
116
|
-
if (!clerk) return false;
|
|
117
|
-
|
|
118
|
-
await clerk.session?.user?.reload();
|
|
119
|
-
|
|
120
|
-
const profile = await getUserProfile(clerk);
|
|
121
|
-
|
|
122
|
-
if (!profile) {
|
|
123
|
-
return false;
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
useAuthState.setState({
|
|
127
|
-
isAuthenticated: true,
|
|
128
|
-
isPending: false,
|
|
129
|
-
profile,
|
|
130
|
-
providerData: {
|
|
131
|
-
type: "clerk",
|
|
132
|
-
user: clerk.session?.user,
|
|
133
|
-
},
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
return true;
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
async function signRequest(request: Request): Promise<Request> {
|
|
140
|
-
const response = await getAccessToken();
|
|
141
|
-
request.headers.set("Authorization", `Bearer ${response}`);
|
|
142
|
-
return request;
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
return {
|
|
146
|
-
getRoutes: () => [
|
|
147
|
-
{ path: "/signout", element: <SignOut /> },
|
|
148
|
-
{ path: "/signin", element: <SignIn /> },
|
|
149
|
-
{ path: "/signup", element: <SignUp /> },
|
|
150
|
-
],
|
|
151
|
-
refreshUserProfile,
|
|
152
|
-
getProfileMenuItems() {
|
|
153
|
-
return [];
|
|
154
|
-
},
|
|
155
|
-
initialize: async () => {
|
|
156
|
-
if (typeof window === "undefined") return;
|
|
157
|
-
|
|
158
|
-
const clerk = await getClerk().catch((e) => {
|
|
159
|
-
// biome-ignore lint/suspicious/noConsole: Intentional error logging
|
|
160
|
-
console.error("Clerk failed to initialize:", e);
|
|
161
|
-
return undefined;
|
|
162
|
-
});
|
|
163
|
-
if (!clerk) return;
|
|
164
|
-
|
|
165
|
-
if (clerk.session) {
|
|
166
|
-
const profile = await getUserProfile(clerk);
|
|
167
|
-
|
|
168
|
-
if (!profile) {
|
|
169
|
-
useAuthState.getState().setLoggedOut();
|
|
170
|
-
return;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
useAuthState.getState().setLoggedIn({
|
|
174
|
-
profile,
|
|
175
|
-
providerData: {
|
|
176
|
-
type: "clerk",
|
|
177
|
-
user: clerk.session.user,
|
|
178
|
-
},
|
|
179
|
-
});
|
|
180
|
-
} else {
|
|
181
|
-
useAuthState.getState().setLoggedOut();
|
|
182
|
-
}
|
|
183
|
-
},
|
|
184
|
-
getAccessToken,
|
|
185
|
-
signRequest,
|
|
186
|
-
signOut: async () => {
|
|
187
|
-
const clerk = await getClerk();
|
|
188
|
-
useAuthState.getState().setLoggedOut();
|
|
189
|
-
await clerk.signOut({
|
|
190
|
-
redirectUrl: window.location.origin + redirectToAfterSignOut,
|
|
191
|
-
});
|
|
192
|
-
},
|
|
193
|
-
signIn: async (
|
|
194
|
-
_: AuthActionContext,
|
|
195
|
-
{ redirectTo }: { redirectTo?: string } = {},
|
|
196
|
-
) => {
|
|
197
|
-
const clerk = await getClerk();
|
|
198
|
-
await clerk.redirectToSignIn({
|
|
199
|
-
signInForceRedirectUrl: redirectToAfterSignIn
|
|
200
|
-
? window.location.origin + redirectToAfterSignIn
|
|
201
|
-
: redirectTo,
|
|
202
|
-
signUpForceRedirectUrl: redirectToAfterSignUp
|
|
203
|
-
? window.location.origin + redirectToAfterSignUp
|
|
204
|
-
: redirectTo,
|
|
205
|
-
});
|
|
206
|
-
},
|
|
207
|
-
signUp: async (
|
|
208
|
-
_: AuthActionContext,
|
|
209
|
-
{ redirectTo }: { redirectTo?: string } = {},
|
|
210
|
-
) => {
|
|
211
|
-
const clerk = await getClerk();
|
|
212
|
-
await clerk.redirectToSignUp({
|
|
213
|
-
signInForceRedirectUrl: redirectToAfterSignIn
|
|
214
|
-
? window.location.origin + redirectToAfterSignIn
|
|
215
|
-
: redirectTo,
|
|
216
|
-
signUpForceRedirectUrl: redirectToAfterSignUp
|
|
217
|
-
? window.location.origin + redirectToAfterSignUp
|
|
218
|
-
: redirectTo,
|
|
219
|
-
});
|
|
220
|
-
},
|
|
221
|
-
};
|
|
222
|
-
};
|
|
223
|
-
|
|
224
|
-
export default clerkAuth;
|
|
1
|
+
import type { Clerk } from "@clerk/clerk-js";
|
|
2
|
+
import type { ZudokuPlugin } from "@lukoweb/apitogo/plugins";
|
|
3
|
+
import type { ClerkAuthenticationConfig } from "../../../config/config.js";
|
|
4
|
+
import type {
|
|
5
|
+
AuthActionContext,
|
|
6
|
+
AuthenticationPlugin,
|
|
7
|
+
AuthenticationProviderInitializer,
|
|
8
|
+
} from "../authentication.js";
|
|
9
|
+
import { SignIn } from "../components/SignIn.js";
|
|
10
|
+
import { SignOut } from "../components/SignOut.js";
|
|
11
|
+
import { SignUp } from "../components/SignUp.js";
|
|
12
|
+
import { type UserProfile, useAuthState } from "../state.js";
|
|
13
|
+
import { getClerkFrontendApi } from "./util.js";
|
|
14
|
+
|
|
15
|
+
export type ClerkProviderData = {
|
|
16
|
+
type: "clerk";
|
|
17
|
+
user: NonNullable<Clerk["session"]>["user"] | undefined;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
declare module "../state.js" {
|
|
21
|
+
interface ProviderDataRegistry {
|
|
22
|
+
clerk: ClerkProviderData;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
let clerkPromise: Promise<Clerk> | undefined;
|
|
27
|
+
|
|
28
|
+
const loadClerk = (publishableKey: string): Promise<Clerk> => {
|
|
29
|
+
if (clerkPromise) return clerkPromise;
|
|
30
|
+
|
|
31
|
+
clerkPromise = new Promise<void>((resolve, reject) => {
|
|
32
|
+
const frontendApiUrl = getClerkFrontendApi(publishableKey);
|
|
33
|
+
|
|
34
|
+
const script = document.createElement("script");
|
|
35
|
+
script.src = `https://${frontendApiUrl}/npm/@clerk/clerk-js@5/dist/clerk.browser.js`;
|
|
36
|
+
script.async = true;
|
|
37
|
+
script.crossOrigin = "anonymous";
|
|
38
|
+
script.dataset.clerkPublishableKey = publishableKey;
|
|
39
|
+
script.onload = () => resolve();
|
|
40
|
+
script.onerror = () => {
|
|
41
|
+
clerkPromise = undefined;
|
|
42
|
+
reject(new Error("Failed to load Clerk from CDN"));
|
|
43
|
+
};
|
|
44
|
+
document.head.appendChild(script);
|
|
45
|
+
}).then(async () => {
|
|
46
|
+
const clerk = (window as { Clerk?: Clerk }).Clerk;
|
|
47
|
+
if (!clerk) {
|
|
48
|
+
throw new Error("Clerk script loaded but window.Clerk is not available");
|
|
49
|
+
}
|
|
50
|
+
await clerk.load();
|
|
51
|
+
return clerk;
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
return clerkPromise;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const clerkAuth: AuthenticationProviderInitializer<
|
|
58
|
+
ClerkAuthenticationConfig
|
|
59
|
+
> = ({
|
|
60
|
+
clerkPubKey,
|
|
61
|
+
jwtTemplateName,
|
|
62
|
+
redirectToAfterSignOut = "/",
|
|
63
|
+
redirectToAfterSignUp,
|
|
64
|
+
redirectToAfterSignIn,
|
|
65
|
+
}): AuthenticationPlugin & ZudokuPlugin => {
|
|
66
|
+
const getClerk = (): Promise<Clerk> => {
|
|
67
|
+
if (typeof window === "undefined") {
|
|
68
|
+
return Promise.reject(new Error("Clerk is not available during SSR"));
|
|
69
|
+
}
|
|
70
|
+
return loadClerk(clerkPubKey);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
async function getAccessToken() {
|
|
74
|
+
const clerk = await getClerk();
|
|
75
|
+
|
|
76
|
+
if (!clerk.session) {
|
|
77
|
+
throw new Error("No session available");
|
|
78
|
+
}
|
|
79
|
+
const response = await clerk.session.getToken({
|
|
80
|
+
template: jwtTemplateName,
|
|
81
|
+
});
|
|
82
|
+
if (!response) {
|
|
83
|
+
throw new Error("Could not get access token from Clerk");
|
|
84
|
+
}
|
|
85
|
+
return response;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async function getUserProfile(
|
|
89
|
+
clerk: Clerk,
|
|
90
|
+
): Promise<UserProfile | undefined> {
|
|
91
|
+
const user = clerk.session?.user;
|
|
92
|
+
if (!user) return undefined;
|
|
93
|
+
|
|
94
|
+
const verifiedEmail = user.emailAddresses.find(
|
|
95
|
+
(email) => email.verification.status === "verified",
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
sub: user.id ?? "",
|
|
100
|
+
email:
|
|
101
|
+
verifiedEmail?.emailAddress ??
|
|
102
|
+
user.emailAddresses[0]?.emailAddress ??
|
|
103
|
+
"",
|
|
104
|
+
name: user.fullName ?? "",
|
|
105
|
+
emailVerified: !!verifiedEmail?.emailAddress,
|
|
106
|
+
pictureUrl: user.imageUrl ?? "",
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async function refreshUserProfile() {
|
|
111
|
+
const clerk = await getClerk().catch((e) => {
|
|
112
|
+
// biome-ignore lint/suspicious/noConsole: Intentional warning
|
|
113
|
+
console.warn("Clerk unavailable during profile refresh:", e);
|
|
114
|
+
return undefined;
|
|
115
|
+
});
|
|
116
|
+
if (!clerk) return false;
|
|
117
|
+
|
|
118
|
+
await clerk.session?.user?.reload();
|
|
119
|
+
|
|
120
|
+
const profile = await getUserProfile(clerk);
|
|
121
|
+
|
|
122
|
+
if (!profile) {
|
|
123
|
+
return false;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
useAuthState.setState({
|
|
127
|
+
isAuthenticated: true,
|
|
128
|
+
isPending: false,
|
|
129
|
+
profile,
|
|
130
|
+
providerData: {
|
|
131
|
+
type: "clerk",
|
|
132
|
+
user: clerk.session?.user,
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
return true;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
async function signRequest(request: Request): Promise<Request> {
|
|
140
|
+
const response = await getAccessToken();
|
|
141
|
+
request.headers.set("Authorization", `Bearer ${response}`);
|
|
142
|
+
return request;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
return {
|
|
146
|
+
getRoutes: () => [
|
|
147
|
+
{ path: "/signout", element: <SignOut /> },
|
|
148
|
+
{ path: "/signin", element: <SignIn /> },
|
|
149
|
+
{ path: "/signup", element: <SignUp /> },
|
|
150
|
+
],
|
|
151
|
+
refreshUserProfile,
|
|
152
|
+
getProfileMenuItems() {
|
|
153
|
+
return [];
|
|
154
|
+
},
|
|
155
|
+
initialize: async () => {
|
|
156
|
+
if (typeof window === "undefined") return;
|
|
157
|
+
|
|
158
|
+
const clerk = await getClerk().catch((e) => {
|
|
159
|
+
// biome-ignore lint/suspicious/noConsole: Intentional error logging
|
|
160
|
+
console.error("Clerk failed to initialize:", e);
|
|
161
|
+
return undefined;
|
|
162
|
+
});
|
|
163
|
+
if (!clerk) return;
|
|
164
|
+
|
|
165
|
+
if (clerk.session) {
|
|
166
|
+
const profile = await getUserProfile(clerk);
|
|
167
|
+
|
|
168
|
+
if (!profile) {
|
|
169
|
+
useAuthState.getState().setLoggedOut();
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
useAuthState.getState().setLoggedIn({
|
|
174
|
+
profile,
|
|
175
|
+
providerData: {
|
|
176
|
+
type: "clerk",
|
|
177
|
+
user: clerk.session.user,
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
} else {
|
|
181
|
+
useAuthState.getState().setLoggedOut();
|
|
182
|
+
}
|
|
183
|
+
},
|
|
184
|
+
getAccessToken,
|
|
185
|
+
signRequest,
|
|
186
|
+
signOut: async () => {
|
|
187
|
+
const clerk = await getClerk();
|
|
188
|
+
useAuthState.getState().setLoggedOut();
|
|
189
|
+
await clerk.signOut({
|
|
190
|
+
redirectUrl: window.location.origin + redirectToAfterSignOut,
|
|
191
|
+
});
|
|
192
|
+
},
|
|
193
|
+
signIn: async (
|
|
194
|
+
_: AuthActionContext,
|
|
195
|
+
{ redirectTo }: { redirectTo?: string } = {},
|
|
196
|
+
) => {
|
|
197
|
+
const clerk = await getClerk();
|
|
198
|
+
await clerk.redirectToSignIn({
|
|
199
|
+
signInForceRedirectUrl: redirectToAfterSignIn
|
|
200
|
+
? window.location.origin + redirectToAfterSignIn
|
|
201
|
+
: redirectTo,
|
|
202
|
+
signUpForceRedirectUrl: redirectToAfterSignUp
|
|
203
|
+
? window.location.origin + redirectToAfterSignUp
|
|
204
|
+
: redirectTo,
|
|
205
|
+
});
|
|
206
|
+
},
|
|
207
|
+
signUp: async (
|
|
208
|
+
_: AuthActionContext,
|
|
209
|
+
{ redirectTo }: { redirectTo?: string } = {},
|
|
210
|
+
) => {
|
|
211
|
+
const clerk = await getClerk();
|
|
212
|
+
await clerk.redirectToSignUp({
|
|
213
|
+
signInForceRedirectUrl: redirectToAfterSignIn
|
|
214
|
+
? window.location.origin + redirectToAfterSignIn
|
|
215
|
+
: redirectTo,
|
|
216
|
+
signUpForceRedirectUrl: redirectToAfterSignUp
|
|
217
|
+
? window.location.origin + redirectToAfterSignUp
|
|
218
|
+
: redirectTo,
|
|
219
|
+
});
|
|
220
|
+
},
|
|
221
|
+
};
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
export default clerkAuth;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Card } from "@lukoweb/apitogo/ui/Card.js";
|
|
2
|
-
import createVariantComponent from "../../util/createVariantComponent.js";
|
|
3
|
-
|
|
4
|
-
export const AuthCard = createVariantComponent(
|
|
5
|
-
Card,
|
|
6
|
-
"max-w-md w-full mt-10 mx-auto",
|
|
7
|
-
);
|
|
1
|
+
import { Card } from "@lukoweb/apitogo/ui/Card.js";
|
|
2
|
+
import createVariantComponent from "../../util/createVariantComponent.js";
|
|
3
|
+
|
|
4
|
+
export const AuthCard = createVariantComponent(
|
|
5
|
+
Card,
|
|
6
|
+
"max-w-md w-full mt-10 mx-auto",
|
|
7
|
+
);
|