@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.
Files changed (91) hide show
  1. package/dist/cli/cli.js +168 -57
  2. package/dist/declarations/lib/ui/Command.d.ts +1 -1
  3. package/dist/declarations/lib/util/MdxComponents.d.ts +1 -1
  4. package/package.json +2 -1
  5. package/src/app/main.tsx +150 -150
  6. package/src/config/loader.ts +245 -245
  7. package/src/index.ts +33 -33
  8. package/src/lib/authentication/components/CallbackHandler.tsx +45 -45
  9. package/src/lib/authentication/components/SignIn.tsx +46 -46
  10. package/src/lib/authentication/components/SignUp.tsx +48 -48
  11. package/src/lib/authentication/providers/clerk.tsx +224 -224
  12. package/src/lib/authentication/ui/AuthCard.tsx +7 -7
  13. package/src/lib/authentication/ui/EmailLinkCallbackUi.tsx +129 -125
  14. package/src/lib/authentication/ui/EmailLinkSentUi.tsx +105 -101
  15. package/src/lib/authentication/ui/EmailLinkSignInUi.tsx +100 -96
  16. package/src/lib/authentication/ui/EmailVerificationUi.tsx +128 -124
  17. package/src/lib/authentication/ui/ZudokuAuthUi.tsx +620 -616
  18. package/src/lib/components/AiAssistantMenuItems.tsx +102 -102
  19. package/src/lib/components/Autocomplete.tsx +123 -123
  20. package/src/lib/components/DeveloperHint.tsx +39 -35
  21. package/src/lib/components/Header.tsx +260 -260
  22. package/src/lib/components/Layout.tsx +53 -53
  23. package/src/lib/components/Main.tsx +51 -51
  24. package/src/lib/components/Mermaid.tsx +74 -70
  25. package/src/lib/components/MobileTopNavigation.tsx +276 -276
  26. package/src/lib/components/MultiSelect.tsx +86 -82
  27. package/src/lib/components/TopNavigation.tsx +103 -103
  28. package/src/lib/components/index.ts +26 -26
  29. package/src/lib/components/navigation/NavigationCategory.tsx +157 -157
  30. package/src/lib/components/navigation/NavigationFilterInput.tsx +35 -35
  31. package/src/lib/components/navigation/NavigationItem.tsx +181 -177
  32. package/src/lib/core/RouteGuard.tsx +193 -193
  33. package/src/lib/errors/ErrorMessage.tsx +50 -46
  34. package/src/lib/plugins/api-catalog/Catalog.tsx +80 -80
  35. package/src/lib/plugins/api-keys/CreateApiKey.tsx +128 -124
  36. package/src/lib/plugins/api-keys/CreateApiKeyDialog.tsx +49 -49
  37. package/src/lib/plugins/api-keys/SettingsApiKeys.tsx +72 -72
  38. package/src/lib/plugins/api-keys/settings/ApiKeyItem.tsx +342 -342
  39. package/src/lib/plugins/api-keys/settings/RevealApiKey.tsx +127 -127
  40. package/src/lib/plugins/markdown/MdxPage.tsx +285 -285
  41. package/src/lib/plugins/openapi/ApiHeader.tsx +96 -96
  42. package/src/lib/plugins/openapi/CollapsibleCode.tsx +88 -88
  43. package/src/lib/plugins/openapi/DownloadSchemaButton.tsx +94 -94
  44. package/src/lib/plugins/openapi/GeneratedExampleSidecarBox.tsx +52 -52
  45. package/src/lib/plugins/openapi/OperationListItem.tsx +183 -183
  46. package/src/lib/plugins/openapi/RequestBodySidecarBox.tsx +63 -63
  47. package/src/lib/plugins/openapi/ResponsesSidecarBox.tsx +120 -117
  48. package/src/lib/plugins/openapi/SchemaInfo.tsx +344 -344
  49. package/src/lib/plugins/openapi/SchemaList.tsx +113 -113
  50. package/src/lib/plugins/openapi/Sidecar.tsx +312 -309
  51. package/src/lib/plugins/openapi/SidecarExamples.tsx +177 -174
  52. package/src/lib/plugins/openapi/components/EnumValues.tsx +58 -58
  53. package/src/lib/plugins/openapi/components/ResponseContent.tsx +117 -114
  54. package/src/lib/plugins/openapi/playground/BodyPanel.tsx +274 -271
  55. package/src/lib/plugins/openapi/playground/CollapsibleHeader.tsx +51 -51
  56. package/src/lib/plugins/openapi/playground/ExamplesDropdown.tsx +60 -60
  57. package/src/lib/plugins/openapi/playground/Headers.tsx +188 -181
  58. package/src/lib/plugins/openapi/playground/IdentityDialog.tsx +75 -75
  59. package/src/lib/plugins/openapi/playground/IdentitySelector.tsx +38 -38
  60. package/src/lib/plugins/openapi/playground/ParamsGrid.tsx +45 -45
  61. package/src/lib/plugins/openapi/playground/Playground.tsx +602 -599
  62. package/src/lib/plugins/openapi/playground/PlaygroundDialog.tsx +56 -56
  63. package/src/lib/plugins/openapi/playground/QueryParams.tsx +151 -148
  64. package/src/lib/plugins/openapi/playground/RequestLoginDialog.tsx +70 -70
  65. package/src/lib/plugins/openapi/playground/request-panel/MultipartField.tsx +91 -91
  66. package/src/lib/plugins/openapi/playground/result-panel/AudioPlayer.tsx +50 -50
  67. package/src/lib/plugins/openapi/playground/result-panel/ResponseTab.tsx +355 -355
  68. package/src/lib/plugins/openapi/playground/result-panel/ResultPanel.tsx +106 -102
  69. package/src/lib/plugins/openapi/schema/SchemaPropertyItem.tsx +189 -184
  70. package/src/lib/plugins/openapi/schema/SchemaView.tsx +299 -299
  71. package/src/lib/plugins/search-pagefind/IndexingDialog.tsx +162 -162
  72. package/src/lib/plugins/search-pagefind/PagefindSearch.tsx +202 -202
  73. package/src/lib/plugins/search-pagefind/ResultList.tsx +122 -118
  74. package/src/lib/ui/ActionButton.tsx +21 -21
  75. package/src/lib/ui/Command.tsx +191 -191
  76. package/src/lib/util/MdxComponents.tsx +150 -150
  77. package/src/vite/config.ts +234 -231
  78. package/src/vite/dev-server.ts +291 -291
  79. package/src/vite/plugin-api-keys.ts +50 -50
  80. package/src/vite/plugin-api.ts +320 -318
  81. package/src/vite/plugin-auth.ts +38 -38
  82. package/src/vite/plugin-component.ts +45 -39
  83. package/src/vite/plugin-config.ts +75 -75
  84. package/src/vite/plugin-custom-pages.ts +36 -36
  85. package/src/vite/plugin-docs.ts +202 -202
  86. package/src/vite/plugin-markdown-export.ts +214 -214
  87. package/src/vite/plugin-mdx.ts +149 -149
  88. package/src/vite/plugin-navigation.ts +106 -106
  89. package/src/vite/plugin-search.ts +47 -47
  90. package/src/vite/plugin.ts +42 -42
  91. package/src/vite/prerender/prerender.ts +233 -233
@@ -1,616 +1,620 @@
1
- import { useMutation } from "@tanstack/react-query";
2
- import React from "react";
3
- import { useForm } from "react-hook-form";
4
- import { Link, useNavigate, useSearchParams } from "react-router";
5
- import { ActionButton } from "@lukoweb/apitogo/ui/ActionButton.js";
6
- import { Alert, AlertDescription, AlertTitle } from "@lukoweb/apitogo/ui/Alert.js";
7
- import { Button, type ButtonProps } from "@lukoweb/apitogo/ui/Button.js";
8
- import {
9
- CardContent,
10
- CardDescription,
11
- CardHeader,
12
- CardTitle,
13
- } from "@lukoweb/apitogo/ui/Card.js";
14
- import { Input } from "@lukoweb/apitogo/ui/Input.js";
15
- import { Separator } from "@lukoweb/apitogo/ui/Separator.js";
16
- import {
17
- Form,
18
- FormControl,
19
- FormItem,
20
- FormLabel,
21
- FormMessage,
22
- } from "../../ui/Form.js";
23
- import { cn } from "../../util/cn.js";
24
- import { getRelativeRedirectUrl } from "../utils/relativeRedirectUrl.js";
25
- import { AuthCard } from "./AuthCard.js";
26
- import AppleIcon from "./icons/Apple.js";
27
- import FacebookIcon from "./icons/Facebook.js";
28
- import GithubIcon from "./icons/Github.js";
29
- import GoogleIcon from "./icons/Google.js";
30
- import MicrosoftIcon from "./icons/Microsoft.js";
31
- import XIcon from "./icons/X.js";
32
-
33
- export const AUTH_PROVIDER_NAMES: Record<AuthProviderId, string> = {
34
- google: "Google",
35
- github: "GitHub",
36
- facebook: "Facebook",
37
- twitter: "X",
38
- x: "X",
39
- microsoft: "Microsoft",
40
- apple: "Apple",
41
- yahoo: "Yahoo",
42
- };
43
-
44
- export type AuthProviderId = keyof typeof ProviderIcons;
45
-
46
- const ProviderIcons = {
47
- google: GoogleIcon,
48
- github: GithubIcon,
49
- facebook: FacebookIcon,
50
- twitter: XIcon,
51
- x: XIcon,
52
- apple: AppleIcon,
53
- microsoft: MicrosoftIcon,
54
- yahoo: React.Fragment,
55
- } as const;
56
-
57
- const isValidAuthProviderId = (
58
- provider: string,
59
- ): provider is AuthProviderId => {
60
- return provider in ProviderIcons;
61
- };
62
-
63
- const isAuthProviderIdArray = (
64
- providers: string[],
65
- ): providers is AuthProviderId[] => {
66
- return providers.every(isValidAuthProviderId);
67
- };
68
-
69
- const AuthProviderButton = ({
70
- providerId,
71
- onClick,
72
- ...buttonProps
73
- }: { providerId: AuthProviderId; onClick?: () => void } & ButtonProps) => {
74
- const IconRenderer = ProviderIcons[providerId];
75
- return (
76
- <Button
77
- variant="outline"
78
- className="gap-2"
79
- onClick={onClick}
80
- {...buttonProps}
81
- >
82
- <IconRenderer className="w-4 h-4 fill-foreground" />
83
- {AUTH_PROVIDER_NAMES[providerId]}
84
- </Button>
85
- );
86
- };
87
-
88
- type FormFields = {
89
- email: string;
90
- password: string;
91
- };
92
-
93
- const EmailPasswordForm = ({
94
- form,
95
- onSubmit,
96
- submitLabel,
97
- isPending,
98
- }: {
99
- form: ReturnType<typeof useForm<FormFields>>;
100
- onSubmit: (data: FormFields) => void;
101
- submitLabel: string;
102
- isPending: boolean;
103
- }) => {
104
- return (
105
- <Form {...form}>
106
- <form
107
- onSubmit={form.handleSubmit(onSubmit)}
108
- className="flex flex-col gap-2"
109
- >
110
- <FormItem>
111
- <FormLabel>E-Mail</FormLabel>
112
- <FormControl>
113
- <Input placeholder="Email" {...form.register("email")} />
114
- </FormControl>
115
- <FormMessage />
116
- </FormItem>
117
- <FormItem>
118
- <FormLabel>Password</FormLabel>
119
- <FormControl>
120
- <Input
121
- placeholder="Password"
122
- {...form.register("password")}
123
- type="password"
124
- />
125
- </FormControl>
126
- <FormMessage />
127
- </FormItem>
128
- <ActionButton type="submit" isPending={isPending}>
129
- {submitLabel}
130
- </ActionButton>
131
- </form>
132
- </Form>
133
- );
134
- };
135
-
136
- export const ZudokuSignInUi = ({
137
- providers,
138
- onOAuthSignIn,
139
- onUsernamePasswordSignIn,
140
- enableUsernamePassword,
141
- enableEmailLink,
142
- }: {
143
- providers: string[];
144
- enableUsernamePassword: boolean;
145
- enableEmailLink?: boolean;
146
- onOAuthSignIn: (providerId: string) => Promise<void>;
147
- onUsernamePasswordSignIn: (email: string, password: string) => Promise<void>;
148
- }) => {
149
- const navigate = useNavigate();
150
- const [searchParams] = useSearchParams();
151
- const redirectTo = searchParams.get("redirectTo");
152
-
153
- const relativeRedirectTo = getRelativeRedirectUrl(redirectTo);
154
-
155
- const invalidProviders = providers.filter(
156
- (provider) => !isValidAuthProviderId(provider),
157
- );
158
-
159
- if (invalidProviders.length > 0) {
160
- throw new Error(
161
- `Unsupported auth provider: ${invalidProviders.join(", ")}`,
162
- );
163
- }
164
-
165
- if (!isAuthProviderIdArray(providers)) {
166
- throw new Error(`Unsupported auth provider: ${providers.join(", ")}`);
167
- }
168
-
169
- const signInUsernameMutation = useMutation({
170
- mutationFn: ({ email, password }: FormFields) =>
171
- onUsernamePasswordSignIn(email, password),
172
- onSuccess: () => {
173
- void navigate(relativeRedirectTo);
174
- },
175
- });
176
- const signInByProviderMutation = useMutation({
177
- mutationFn: ({ providerId }: { providerId: string }) =>
178
- onOAuthSignIn(providerId),
179
- onSuccess: () => {
180
- void navigate(relativeRedirectTo);
181
- },
182
- });
183
- const form = useForm<FormFields>({
184
- defaultValues: {
185
- email: "",
186
- password: "",
187
- },
188
- });
189
-
190
- const pending =
191
- signInUsernameMutation.isPending || signInByProviderMutation.isPending;
192
-
193
- const error = signInUsernameMutation.error ?? signInByProviderMutation.error;
194
-
195
- const hasEmailMethod = enableUsernamePassword || enableEmailLink;
196
-
197
- return (
198
- <AuthCard>
199
- <CardHeader>
200
- <CardTitle>Sign in</CardTitle>
201
- <CardDescription>Sign in to your account to continue.</CardDescription>
202
- </CardHeader>
203
- <CardContent className="flex flex-col gap-4">
204
- {error && (
205
- <Alert variant="destructive">
206
- <AlertTitle>Error</AlertTitle>
207
- <AlertDescription>{error?.message}</AlertDescription>
208
- </Alert>
209
- )}
210
- {enableUsernamePassword && (
211
- <>
212
- <EmailPasswordForm
213
- form={form}
214
- onSubmit={(data) =>
215
- void signInUsernameMutation.mutate({
216
- email: data.email,
217
- password: data.password,
218
- })
219
- }
220
- submitLabel="Sign in"
221
- isPending={pending}
222
- />
223
- <Link
224
- to="/reset-password"
225
- className="text-sm text-muted-foreground text-right -mt-2"
226
- >
227
- Forgot password?
228
- </Link>
229
- </>
230
- )}
231
- {hasEmailMethod && providers.length > 0 && (
232
- <ProviderSeparator providers={providers} />
233
- )}
234
- {providers.length > 0 && (
235
- <ProviderButtons
236
- providers={providers}
237
- onClick={(providerId) =>
238
- signInByProviderMutation.mutate({ providerId })
239
- }
240
- />
241
- )}
242
- <div className="flex flex-col gap-1">
243
- {enableEmailLink && (
244
- <Link
245
- to={
246
- redirectTo
247
- ? `/signin/email-link?redirectTo=${encodeURIComponent(redirectTo)}`
248
- : "/signin/email-link"
249
- }
250
- className="text-sm text-muted-foreground"
251
- >
252
- Sign in with email link
253
- </Link>
254
- )}
255
- <Link to="/signup" className="text-sm text-muted-foreground">
256
- Don't have an account? Sign up.
257
- </Link>
258
- </div>
259
- </CardContent>
260
- </AuthCard>
261
- );
262
- };
263
-
264
- export const ZudokuSignUpUi = ({
265
- providers,
266
- enableUsernamePassword,
267
- enableEmailLink,
268
- onOAuthSignUp,
269
- onUsernamePasswordSignUp,
270
- }: {
271
- providers: string[];
272
- enableUsernamePassword: boolean;
273
- enableEmailLink?: boolean;
274
- onOAuthSignUp: (providerId: string) => Promise<void>;
275
- onUsernamePasswordSignUp: (email: string, password: string) => Promise<void>;
276
- }) => {
277
- const navigate = useNavigate();
278
- const [searchParams] = useSearchParams();
279
- const redirectTo = searchParams.get("redirectTo");
280
-
281
- const relativeRedirectTo = getRelativeRedirectUrl(redirectTo);
282
-
283
- if (!isAuthProviderIdArray(providers)) {
284
- throw new Error("Invalid auth provider IDs");
285
- }
286
-
287
- const signUpUsernameMutation = useMutation({
288
- mutationFn: async ({ email, password }: FormFields) => {
289
- await onUsernamePasswordSignUp(email, password);
290
- },
291
- onSuccess: () => {
292
- void navigate(relativeRedirectTo);
293
- },
294
- });
295
-
296
- const signUpByProviderMutation = useMutation({
297
- mutationFn: async ({ providerId }: { providerId: string }) => {
298
- await onOAuthSignUp(providerId);
299
- },
300
- onSuccess: () => {
301
- void navigate(relativeRedirectTo);
302
- },
303
- });
304
-
305
- const form = useForm<FormFields>({
306
- defaultValues: {
307
- email: "",
308
- password: "",
309
- },
310
- });
311
-
312
- const pending =
313
- signUpUsernameMutation.isPending || signUpByProviderMutation.isPending;
314
-
315
- const error = signUpUsernameMutation.error ?? signUpByProviderMutation.error;
316
-
317
- const hasEmailMethod = enableUsernamePassword || enableEmailLink;
318
-
319
- return (
320
- <AuthCard>
321
- <CardHeader>
322
- <CardTitle>Sign up</CardTitle>
323
- <CardDescription>Sign up to your account to continue.</CardDescription>
324
- </CardHeader>
325
- <CardContent className="flex flex-col gap-4">
326
- {error && (
327
- <Alert variant="destructive">
328
- <AlertTitle>Error</AlertTitle>
329
- <AlertDescription>{error?.message}</AlertDescription>
330
- </Alert>
331
- )}
332
-
333
- {enableUsernamePassword && (
334
- <EmailPasswordForm
335
- form={form}
336
- onSubmit={(data) =>
337
- void signUpUsernameMutation.mutate({
338
- email: data.email,
339
- password: data.password,
340
- })
341
- }
342
- submitLabel="Sign up"
343
- isPending={pending}
344
- />
345
- )}
346
- {hasEmailMethod && providers.length > 0 && (
347
- <ProviderSeparator providers={providers} />
348
- )}
349
- {providers.length > 0 && (
350
- <ProviderButtons
351
- providers={providers}
352
- onClick={(providerId) =>
353
- signUpByProviderMutation.mutate({ providerId })
354
- }
355
- />
356
- )}
357
- <div className="flex flex-col gap-1">
358
- {enableEmailLink && (
359
- <Link
360
- to={
361
- redirectTo
362
- ? `/signin/email-link?redirectTo=${encodeURIComponent(redirectTo)}`
363
- : "/signin/email-link"
364
- }
365
- className="text-sm text-muted-foreground"
366
- >
367
- Sign in with email link
368
- </Link>
369
- )}
370
- <Link to="/signin" className="text-sm text-muted-foreground">
371
- Already have an account? Sign in.
372
- </Link>
373
- </div>
374
- </CardContent>
375
- </AuthCard>
376
- );
377
- };
378
-
379
- const ProviderButtons = ({
380
- providers,
381
- onClick,
382
- }: {
383
- providers: AuthProviderId[];
384
- onClick: (providerId: string) => void;
385
- }) => {
386
- return (
387
- <div
388
- className={cn(
389
- "grid grid-cols-2 gap-2",
390
- providers.length % 2 === 0 ? "grid-cols-2" : "grid-cols-1",
391
- )}
392
- >
393
- {providers.map((provider) => (
394
- <AuthProviderButton
395
- key={provider}
396
- providerId={provider}
397
- onClick={() => onClick(provider)}
398
- />
399
- ))}
400
- </div>
401
- );
402
- };
403
-
404
- const ProviderSeparator = ({ providers }: { providers: AuthProviderId[] }) => {
405
- return (
406
- providers.length > 0 && (
407
- <Separator className="my-3 relative">
408
- <span className="bg-card text-muted-foreground text-sm px-2 absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2">
409
- or continue with
410
- </span>
411
- </Separator>
412
- )
413
- );
414
- };
415
-
416
- export const ZudokuPasswordResetUi = ({
417
- onPasswordReset,
418
- }: {
419
- onPasswordReset: (email: string) => Promise<void>;
420
- }) => {
421
- const [isSubmitted, setIsSubmitted] = React.useState(false);
422
-
423
- const passwordResetMutation = useMutation({
424
- mutationFn: async ({ email }: { email: string }) => {
425
- await onPasswordReset(email);
426
- },
427
- onSuccess: () => {
428
- setIsSubmitted(true);
429
- },
430
- });
431
-
432
- const form = useForm<{ email: string }>({
433
- defaultValues: {
434
- email: "",
435
- },
436
- });
437
-
438
- const error = passwordResetMutation.error;
439
-
440
- return (
441
- <AuthCard>
442
- <CardHeader>
443
- <CardTitle>Reset password</CardTitle>
444
- <CardDescription>
445
- {isSubmitted
446
- ? "Check your email for a password reset link."
447
- : "Enter your email address and we'll send you a link to reset your password."}
448
- </CardDescription>
449
- </CardHeader>
450
- <CardContent className="flex flex-col gap-4">
451
- {error && (
452
- <Alert variant="destructive">
453
- <AlertTitle>Error</AlertTitle>
454
- <AlertDescription>{error?.message}</AlertDescription>
455
- </Alert>
456
- )}
457
- {isSubmitted ? (
458
- <div className="flex flex-col gap-4">
459
- <Alert>
460
- <AlertTitle>Email sent</AlertTitle>
461
- <AlertDescription>
462
- If an account exists with that email address, you will receive a
463
- password reset link shortly.
464
- </AlertDescription>
465
- </Alert>
466
- <Link to="/signin">
467
- <Button variant="outline" className="w-full">
468
- Back to sign in
469
- </Button>
470
- </Link>
471
- </div>
472
- ) : (
473
- <>
474
- <Form {...form}>
475
- <form
476
- onSubmit={form.handleSubmit((data) =>
477
- passwordResetMutation.mutate({ email: data.email }),
478
- )}
479
- className="flex flex-col gap-2"
480
- >
481
- <FormItem>
482
- <FormLabel>Email</FormLabel>
483
- <FormControl>
484
- <Input
485
- placeholder="you@example.com"
486
- {...form.register("email")}
487
- />
488
- </FormControl>
489
- <FormMessage />
490
- </FormItem>
491
- <ActionButton
492
- type="submit"
493
- isPending={passwordResetMutation.isPending}
494
- >
495
- Reset password
496
- </ActionButton>
497
- </form>
498
- </Form>
499
- <Link to="/signin" className="text-sm text-muted-foreground">
500
- Sign in
501
- </Link>
502
- </>
503
- )}
504
- </CardContent>
505
- </AuthCard>
506
- );
507
- };
508
-
509
- export const ZudokuPasswordUpdateUi = ({
510
- onPasswordUpdate,
511
- }: {
512
- onPasswordUpdate: (password: string) => Promise<void>;
513
- }) => {
514
- const [isSubmitted, setIsSubmitted] = React.useState(false);
515
-
516
- const passwordUpdateMutation = useMutation({
517
- mutationFn: async ({ password }: { password: string }) => {
518
- await onPasswordUpdate(password);
519
- },
520
- onSuccess: () => {
521
- setIsSubmitted(true);
522
- },
523
- });
524
-
525
- const form = useForm<{ password: string; confirmPassword: string }>({
526
- defaultValues: {
527
- password: "",
528
- confirmPassword: "",
529
- },
530
- });
531
-
532
- const error = passwordUpdateMutation.error;
533
-
534
- const onSubmit = (data: { password: string; confirmPassword: string }) => {
535
- if (data.password !== data.confirmPassword) {
536
- form.setError("confirmPassword", {
537
- message: "Passwords do not match",
538
- });
539
- return;
540
- }
541
- passwordUpdateMutation.mutate({ password: data.password });
542
- };
543
-
544
- return (
545
- <AuthCard>
546
- <CardHeader>
547
- <CardTitle>Set new password</CardTitle>
548
- <CardDescription>
549
- {isSubmitted
550
- ? "Your password has been updated successfully."
551
- : "Enter your new password below."}
552
- </CardDescription>
553
- </CardHeader>
554
- <CardContent className="flex flex-col gap-4">
555
- {error && (
556
- <Alert variant="destructive">
557
- <AlertTitle>Error</AlertTitle>
558
- <AlertDescription>{error?.message}</AlertDescription>
559
- </Alert>
560
- )}
561
- {isSubmitted ? (
562
- <div className="flex flex-col gap-4">
563
- <Alert>
564
- <AlertTitle>Password updated</AlertTitle>
565
- <AlertDescription>
566
- Your password has been successfully updated. You can now sign in
567
- with your new password.
568
- </AlertDescription>
569
- </Alert>
570
- <Link to="/signin">
571
- <Button variant="outline" className="w-full">
572
- Sign in
573
- </Button>
574
- </Link>
575
- </div>
576
- ) : (
577
- <Form {...form}>
578
- <form
579
- onSubmit={form.handleSubmit(onSubmit)}
580
- className="flex flex-col gap-2"
581
- >
582
- <FormItem>
583
- <FormLabel>New password</FormLabel>
584
- <FormControl>
585
- <Input
586
- type="password"
587
- placeholder="Enter new password"
588
- {...form.register("password")}
589
- />
590
- </FormControl>
591
- <FormMessage />
592
- </FormItem>
593
- <FormItem>
594
- <FormLabel>Confirm password</FormLabel>
595
- <FormControl>
596
- <Input
597
- type="password"
598
- placeholder="Confirm new password"
599
- {...form.register("confirmPassword")}
600
- />
601
- </FormControl>
602
- <FormMessage />
603
- </FormItem>
604
- <ActionButton
605
- type="submit"
606
- isPending={passwordUpdateMutation.isPending}
607
- >
608
- Update password
609
- </ActionButton>
610
- </form>
611
- </Form>
612
- )}
613
- </CardContent>
614
- </AuthCard>
615
- );
616
- };
1
+ import { ActionButton } from "@lukoweb/apitogo/ui/ActionButton.js";
2
+ import {
3
+ Alert,
4
+ AlertDescription,
5
+ AlertTitle,
6
+ } from "@lukoweb/apitogo/ui/Alert.js";
7
+ import { Button, type ButtonProps } from "@lukoweb/apitogo/ui/Button.js";
8
+ import {
9
+ CardContent,
10
+ CardDescription,
11
+ CardHeader,
12
+ CardTitle,
13
+ } from "@lukoweb/apitogo/ui/Card.js";
14
+ import { Input } from "@lukoweb/apitogo/ui/Input.js";
15
+ import { Separator } from "@lukoweb/apitogo/ui/Separator.js";
16
+ import { useMutation } from "@tanstack/react-query";
17
+ import React from "react";
18
+ import { useForm } from "react-hook-form";
19
+ import { Link, useNavigate, useSearchParams } from "react-router";
20
+ import {
21
+ Form,
22
+ FormControl,
23
+ FormItem,
24
+ FormLabel,
25
+ FormMessage,
26
+ } from "../../ui/Form.js";
27
+ import { cn } from "../../util/cn.js";
28
+ import { getRelativeRedirectUrl } from "../utils/relativeRedirectUrl.js";
29
+ import { AuthCard } from "./AuthCard.js";
30
+ import AppleIcon from "./icons/Apple.js";
31
+ import FacebookIcon from "./icons/Facebook.js";
32
+ import GithubIcon from "./icons/Github.js";
33
+ import GoogleIcon from "./icons/Google.js";
34
+ import MicrosoftIcon from "./icons/Microsoft.js";
35
+ import XIcon from "./icons/X.js";
36
+
37
+ export const AUTH_PROVIDER_NAMES: Record<AuthProviderId, string> = {
38
+ google: "Google",
39
+ github: "GitHub",
40
+ facebook: "Facebook",
41
+ twitter: "X",
42
+ x: "X",
43
+ microsoft: "Microsoft",
44
+ apple: "Apple",
45
+ yahoo: "Yahoo",
46
+ };
47
+
48
+ export type AuthProviderId = keyof typeof ProviderIcons;
49
+
50
+ const ProviderIcons = {
51
+ google: GoogleIcon,
52
+ github: GithubIcon,
53
+ facebook: FacebookIcon,
54
+ twitter: XIcon,
55
+ x: XIcon,
56
+ apple: AppleIcon,
57
+ microsoft: MicrosoftIcon,
58
+ yahoo: React.Fragment,
59
+ } as const;
60
+
61
+ const isValidAuthProviderId = (
62
+ provider: string,
63
+ ): provider is AuthProviderId => {
64
+ return provider in ProviderIcons;
65
+ };
66
+
67
+ const isAuthProviderIdArray = (
68
+ providers: string[],
69
+ ): providers is AuthProviderId[] => {
70
+ return providers.every(isValidAuthProviderId);
71
+ };
72
+
73
+ const AuthProviderButton = ({
74
+ providerId,
75
+ onClick,
76
+ ...buttonProps
77
+ }: { providerId: AuthProviderId; onClick?: () => void } & ButtonProps) => {
78
+ const IconRenderer = ProviderIcons[providerId];
79
+ return (
80
+ <Button
81
+ variant="outline"
82
+ className="gap-2"
83
+ onClick={onClick}
84
+ {...buttonProps}
85
+ >
86
+ <IconRenderer className="w-4 h-4 fill-foreground" />
87
+ {AUTH_PROVIDER_NAMES[providerId]}
88
+ </Button>
89
+ );
90
+ };
91
+
92
+ type FormFields = {
93
+ email: string;
94
+ password: string;
95
+ };
96
+
97
+ const EmailPasswordForm = ({
98
+ form,
99
+ onSubmit,
100
+ submitLabel,
101
+ isPending,
102
+ }: {
103
+ form: ReturnType<typeof useForm<FormFields>>;
104
+ onSubmit: (data: FormFields) => void;
105
+ submitLabel: string;
106
+ isPending: boolean;
107
+ }) => {
108
+ return (
109
+ <Form {...form}>
110
+ <form
111
+ onSubmit={form.handleSubmit(onSubmit)}
112
+ className="flex flex-col gap-2"
113
+ >
114
+ <FormItem>
115
+ <FormLabel>E-Mail</FormLabel>
116
+ <FormControl>
117
+ <Input placeholder="Email" {...form.register("email")} />
118
+ </FormControl>
119
+ <FormMessage />
120
+ </FormItem>
121
+ <FormItem>
122
+ <FormLabel>Password</FormLabel>
123
+ <FormControl>
124
+ <Input
125
+ placeholder="Password"
126
+ {...form.register("password")}
127
+ type="password"
128
+ />
129
+ </FormControl>
130
+ <FormMessage />
131
+ </FormItem>
132
+ <ActionButton type="submit" isPending={isPending}>
133
+ {submitLabel}
134
+ </ActionButton>
135
+ </form>
136
+ </Form>
137
+ );
138
+ };
139
+
140
+ export const ZudokuSignInUi = ({
141
+ providers,
142
+ onOAuthSignIn,
143
+ onUsernamePasswordSignIn,
144
+ enableUsernamePassword,
145
+ enableEmailLink,
146
+ }: {
147
+ providers: string[];
148
+ enableUsernamePassword: boolean;
149
+ enableEmailLink?: boolean;
150
+ onOAuthSignIn: (providerId: string) => Promise<void>;
151
+ onUsernamePasswordSignIn: (email: string, password: string) => Promise<void>;
152
+ }) => {
153
+ const navigate = useNavigate();
154
+ const [searchParams] = useSearchParams();
155
+ const redirectTo = searchParams.get("redirectTo");
156
+
157
+ const relativeRedirectTo = getRelativeRedirectUrl(redirectTo);
158
+
159
+ const invalidProviders = providers.filter(
160
+ (provider) => !isValidAuthProviderId(provider),
161
+ );
162
+
163
+ if (invalidProviders.length > 0) {
164
+ throw new Error(
165
+ `Unsupported auth provider: ${invalidProviders.join(", ")}`,
166
+ );
167
+ }
168
+
169
+ if (!isAuthProviderIdArray(providers)) {
170
+ throw new Error(`Unsupported auth provider: ${providers.join(", ")}`);
171
+ }
172
+
173
+ const signInUsernameMutation = useMutation({
174
+ mutationFn: ({ email, password }: FormFields) =>
175
+ onUsernamePasswordSignIn(email, password),
176
+ onSuccess: () => {
177
+ void navigate(relativeRedirectTo);
178
+ },
179
+ });
180
+ const signInByProviderMutation = useMutation({
181
+ mutationFn: ({ providerId }: { providerId: string }) =>
182
+ onOAuthSignIn(providerId),
183
+ onSuccess: () => {
184
+ void navigate(relativeRedirectTo);
185
+ },
186
+ });
187
+ const form = useForm<FormFields>({
188
+ defaultValues: {
189
+ email: "",
190
+ password: "",
191
+ },
192
+ });
193
+
194
+ const pending =
195
+ signInUsernameMutation.isPending || signInByProviderMutation.isPending;
196
+
197
+ const error = signInUsernameMutation.error ?? signInByProviderMutation.error;
198
+
199
+ const hasEmailMethod = enableUsernamePassword || enableEmailLink;
200
+
201
+ return (
202
+ <AuthCard>
203
+ <CardHeader>
204
+ <CardTitle>Sign in</CardTitle>
205
+ <CardDescription>Sign in to your account to continue.</CardDescription>
206
+ </CardHeader>
207
+ <CardContent className="flex flex-col gap-4">
208
+ {error && (
209
+ <Alert variant="destructive">
210
+ <AlertTitle>Error</AlertTitle>
211
+ <AlertDescription>{error?.message}</AlertDescription>
212
+ </Alert>
213
+ )}
214
+ {enableUsernamePassword && (
215
+ <>
216
+ <EmailPasswordForm
217
+ form={form}
218
+ onSubmit={(data) =>
219
+ void signInUsernameMutation.mutate({
220
+ email: data.email,
221
+ password: data.password,
222
+ })
223
+ }
224
+ submitLabel="Sign in"
225
+ isPending={pending}
226
+ />
227
+ <Link
228
+ to="/reset-password"
229
+ className="text-sm text-muted-foreground text-right -mt-2"
230
+ >
231
+ Forgot password?
232
+ </Link>
233
+ </>
234
+ )}
235
+ {hasEmailMethod && providers.length > 0 && (
236
+ <ProviderSeparator providers={providers} />
237
+ )}
238
+ {providers.length > 0 && (
239
+ <ProviderButtons
240
+ providers={providers}
241
+ onClick={(providerId) =>
242
+ signInByProviderMutation.mutate({ providerId })
243
+ }
244
+ />
245
+ )}
246
+ <div className="flex flex-col gap-1">
247
+ {enableEmailLink && (
248
+ <Link
249
+ to={
250
+ redirectTo
251
+ ? `/signin/email-link?redirectTo=${encodeURIComponent(redirectTo)}`
252
+ : "/signin/email-link"
253
+ }
254
+ className="text-sm text-muted-foreground"
255
+ >
256
+ Sign in with email link
257
+ </Link>
258
+ )}
259
+ <Link to="/signup" className="text-sm text-muted-foreground">
260
+ Don't have an account? Sign up.
261
+ </Link>
262
+ </div>
263
+ </CardContent>
264
+ </AuthCard>
265
+ );
266
+ };
267
+
268
+ export const ZudokuSignUpUi = ({
269
+ providers,
270
+ enableUsernamePassword,
271
+ enableEmailLink,
272
+ onOAuthSignUp,
273
+ onUsernamePasswordSignUp,
274
+ }: {
275
+ providers: string[];
276
+ enableUsernamePassword: boolean;
277
+ enableEmailLink?: boolean;
278
+ onOAuthSignUp: (providerId: string) => Promise<void>;
279
+ onUsernamePasswordSignUp: (email: string, password: string) => Promise<void>;
280
+ }) => {
281
+ const navigate = useNavigate();
282
+ const [searchParams] = useSearchParams();
283
+ const redirectTo = searchParams.get("redirectTo");
284
+
285
+ const relativeRedirectTo = getRelativeRedirectUrl(redirectTo);
286
+
287
+ if (!isAuthProviderIdArray(providers)) {
288
+ throw new Error("Invalid auth provider IDs");
289
+ }
290
+
291
+ const signUpUsernameMutation = useMutation({
292
+ mutationFn: async ({ email, password }: FormFields) => {
293
+ await onUsernamePasswordSignUp(email, password);
294
+ },
295
+ onSuccess: () => {
296
+ void navigate(relativeRedirectTo);
297
+ },
298
+ });
299
+
300
+ const signUpByProviderMutation = useMutation({
301
+ mutationFn: async ({ providerId }: { providerId: string }) => {
302
+ await onOAuthSignUp(providerId);
303
+ },
304
+ onSuccess: () => {
305
+ void navigate(relativeRedirectTo);
306
+ },
307
+ });
308
+
309
+ const form = useForm<FormFields>({
310
+ defaultValues: {
311
+ email: "",
312
+ password: "",
313
+ },
314
+ });
315
+
316
+ const pending =
317
+ signUpUsernameMutation.isPending || signUpByProviderMutation.isPending;
318
+
319
+ const error = signUpUsernameMutation.error ?? signUpByProviderMutation.error;
320
+
321
+ const hasEmailMethod = enableUsernamePassword || enableEmailLink;
322
+
323
+ return (
324
+ <AuthCard>
325
+ <CardHeader>
326
+ <CardTitle>Sign up</CardTitle>
327
+ <CardDescription>Sign up to your account to continue.</CardDescription>
328
+ </CardHeader>
329
+ <CardContent className="flex flex-col gap-4">
330
+ {error && (
331
+ <Alert variant="destructive">
332
+ <AlertTitle>Error</AlertTitle>
333
+ <AlertDescription>{error?.message}</AlertDescription>
334
+ </Alert>
335
+ )}
336
+
337
+ {enableUsernamePassword && (
338
+ <EmailPasswordForm
339
+ form={form}
340
+ onSubmit={(data) =>
341
+ void signUpUsernameMutation.mutate({
342
+ email: data.email,
343
+ password: data.password,
344
+ })
345
+ }
346
+ submitLabel="Sign up"
347
+ isPending={pending}
348
+ />
349
+ )}
350
+ {hasEmailMethod && providers.length > 0 && (
351
+ <ProviderSeparator providers={providers} />
352
+ )}
353
+ {providers.length > 0 && (
354
+ <ProviderButtons
355
+ providers={providers}
356
+ onClick={(providerId) =>
357
+ signUpByProviderMutation.mutate({ providerId })
358
+ }
359
+ />
360
+ )}
361
+ <div className="flex flex-col gap-1">
362
+ {enableEmailLink && (
363
+ <Link
364
+ to={
365
+ redirectTo
366
+ ? `/signin/email-link?redirectTo=${encodeURIComponent(redirectTo)}`
367
+ : "/signin/email-link"
368
+ }
369
+ className="text-sm text-muted-foreground"
370
+ >
371
+ Sign in with email link
372
+ </Link>
373
+ )}
374
+ <Link to="/signin" className="text-sm text-muted-foreground">
375
+ Already have an account? Sign in.
376
+ </Link>
377
+ </div>
378
+ </CardContent>
379
+ </AuthCard>
380
+ );
381
+ };
382
+
383
+ const ProviderButtons = ({
384
+ providers,
385
+ onClick,
386
+ }: {
387
+ providers: AuthProviderId[];
388
+ onClick: (providerId: string) => void;
389
+ }) => {
390
+ return (
391
+ <div
392
+ className={cn(
393
+ "grid grid-cols-2 gap-2",
394
+ providers.length % 2 === 0 ? "grid-cols-2" : "grid-cols-1",
395
+ )}
396
+ >
397
+ {providers.map((provider) => (
398
+ <AuthProviderButton
399
+ key={provider}
400
+ providerId={provider}
401
+ onClick={() => onClick(provider)}
402
+ />
403
+ ))}
404
+ </div>
405
+ );
406
+ };
407
+
408
+ const ProviderSeparator = ({ providers }: { providers: AuthProviderId[] }) => {
409
+ return (
410
+ providers.length > 0 && (
411
+ <Separator className="my-3 relative">
412
+ <span className="bg-card text-muted-foreground text-sm px-2 absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2">
413
+ or continue with
414
+ </span>
415
+ </Separator>
416
+ )
417
+ );
418
+ };
419
+
420
+ export const ZudokuPasswordResetUi = ({
421
+ onPasswordReset,
422
+ }: {
423
+ onPasswordReset: (email: string) => Promise<void>;
424
+ }) => {
425
+ const [isSubmitted, setIsSubmitted] = React.useState(false);
426
+
427
+ const passwordResetMutation = useMutation({
428
+ mutationFn: async ({ email }: { email: string }) => {
429
+ await onPasswordReset(email);
430
+ },
431
+ onSuccess: () => {
432
+ setIsSubmitted(true);
433
+ },
434
+ });
435
+
436
+ const form = useForm<{ email: string }>({
437
+ defaultValues: {
438
+ email: "",
439
+ },
440
+ });
441
+
442
+ const error = passwordResetMutation.error;
443
+
444
+ return (
445
+ <AuthCard>
446
+ <CardHeader>
447
+ <CardTitle>Reset password</CardTitle>
448
+ <CardDescription>
449
+ {isSubmitted
450
+ ? "Check your email for a password reset link."
451
+ : "Enter your email address and we'll send you a link to reset your password."}
452
+ </CardDescription>
453
+ </CardHeader>
454
+ <CardContent className="flex flex-col gap-4">
455
+ {error && (
456
+ <Alert variant="destructive">
457
+ <AlertTitle>Error</AlertTitle>
458
+ <AlertDescription>{error?.message}</AlertDescription>
459
+ </Alert>
460
+ )}
461
+ {isSubmitted ? (
462
+ <div className="flex flex-col gap-4">
463
+ <Alert>
464
+ <AlertTitle>Email sent</AlertTitle>
465
+ <AlertDescription>
466
+ If an account exists with that email address, you will receive a
467
+ password reset link shortly.
468
+ </AlertDescription>
469
+ </Alert>
470
+ <Link to="/signin">
471
+ <Button variant="outline" className="w-full">
472
+ Back to sign in
473
+ </Button>
474
+ </Link>
475
+ </div>
476
+ ) : (
477
+ <>
478
+ <Form {...form}>
479
+ <form
480
+ onSubmit={form.handleSubmit((data) =>
481
+ passwordResetMutation.mutate({ email: data.email }),
482
+ )}
483
+ className="flex flex-col gap-2"
484
+ >
485
+ <FormItem>
486
+ <FormLabel>Email</FormLabel>
487
+ <FormControl>
488
+ <Input
489
+ placeholder="you@example.com"
490
+ {...form.register("email")}
491
+ />
492
+ </FormControl>
493
+ <FormMessage />
494
+ </FormItem>
495
+ <ActionButton
496
+ type="submit"
497
+ isPending={passwordResetMutation.isPending}
498
+ >
499
+ Reset password
500
+ </ActionButton>
501
+ </form>
502
+ </Form>
503
+ <Link to="/signin" className="text-sm text-muted-foreground">
504
+ Sign in
505
+ </Link>
506
+ </>
507
+ )}
508
+ </CardContent>
509
+ </AuthCard>
510
+ );
511
+ };
512
+
513
+ export const ZudokuPasswordUpdateUi = ({
514
+ onPasswordUpdate,
515
+ }: {
516
+ onPasswordUpdate: (password: string) => Promise<void>;
517
+ }) => {
518
+ const [isSubmitted, setIsSubmitted] = React.useState(false);
519
+
520
+ const passwordUpdateMutation = useMutation({
521
+ mutationFn: async ({ password }: { password: string }) => {
522
+ await onPasswordUpdate(password);
523
+ },
524
+ onSuccess: () => {
525
+ setIsSubmitted(true);
526
+ },
527
+ });
528
+
529
+ const form = useForm<{ password: string; confirmPassword: string }>({
530
+ defaultValues: {
531
+ password: "",
532
+ confirmPassword: "",
533
+ },
534
+ });
535
+
536
+ const error = passwordUpdateMutation.error;
537
+
538
+ const onSubmit = (data: { password: string; confirmPassword: string }) => {
539
+ if (data.password !== data.confirmPassword) {
540
+ form.setError("confirmPassword", {
541
+ message: "Passwords do not match",
542
+ });
543
+ return;
544
+ }
545
+ passwordUpdateMutation.mutate({ password: data.password });
546
+ };
547
+
548
+ return (
549
+ <AuthCard>
550
+ <CardHeader>
551
+ <CardTitle>Set new password</CardTitle>
552
+ <CardDescription>
553
+ {isSubmitted
554
+ ? "Your password has been updated successfully."
555
+ : "Enter your new password below."}
556
+ </CardDescription>
557
+ </CardHeader>
558
+ <CardContent className="flex flex-col gap-4">
559
+ {error && (
560
+ <Alert variant="destructive">
561
+ <AlertTitle>Error</AlertTitle>
562
+ <AlertDescription>{error?.message}</AlertDescription>
563
+ </Alert>
564
+ )}
565
+ {isSubmitted ? (
566
+ <div className="flex flex-col gap-4">
567
+ <Alert>
568
+ <AlertTitle>Password updated</AlertTitle>
569
+ <AlertDescription>
570
+ Your password has been successfully updated. You can now sign in
571
+ with your new password.
572
+ </AlertDescription>
573
+ </Alert>
574
+ <Link to="/signin">
575
+ <Button variant="outline" className="w-full">
576
+ Sign in
577
+ </Button>
578
+ </Link>
579
+ </div>
580
+ ) : (
581
+ <Form {...form}>
582
+ <form
583
+ onSubmit={form.handleSubmit(onSubmit)}
584
+ className="flex flex-col gap-2"
585
+ >
586
+ <FormItem>
587
+ <FormLabel>New password</FormLabel>
588
+ <FormControl>
589
+ <Input
590
+ type="password"
591
+ placeholder="Enter new password"
592
+ {...form.register("password")}
593
+ />
594
+ </FormControl>
595
+ <FormMessage />
596
+ </FormItem>
597
+ <FormItem>
598
+ <FormLabel>Confirm password</FormLabel>
599
+ <FormControl>
600
+ <Input
601
+ type="password"
602
+ placeholder="Confirm new password"
603
+ {...form.register("confirmPassword")}
604
+ />
605
+ </FormControl>
606
+ <FormMessage />
607
+ </FormItem>
608
+ <ActionButton
609
+ type="submit"
610
+ isPending={passwordUpdateMutation.isPending}
611
+ >
612
+ Update password
613
+ </ActionButton>
614
+ </form>
615
+ </Form>
616
+ )}
617
+ </CardContent>
618
+ </AuthCard>
619
+ );
620
+ };