@krak-stack/auth 0.1.71 → 0.2.1
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/README.md +4 -3
- package/dist/access.d.ts +5 -7
- package/dist/access.js +11 -7
- package/dist/admin/api.group.d.ts +53 -53
- package/dist/admin/api.group.js +15 -15
- package/dist/admin/schema.d.ts +18 -18
- package/dist/admin/schema.js +3 -3
- package/dist/api.d.ts +1995 -533
- package/dist/api.js +7 -7
- package/dist/auth/api.group.d.ts +793 -0
- package/dist/auth/api.group.js +241 -0
- package/dist/auth/schema.d.ts +808 -0
- package/dist/auth/schema.js +628 -0
- package/dist/components/admin-organization-form.d.ts +1 -1
- package/dist/components/admin-organization-form.js +17 -18
- package/dist/components/admin-organizations.d.ts +4 -5
- package/dist/components/admin-organizations.js +83 -73
- package/dist/components/admin-users.d.ts +5 -6
- package/dist/components/admin-users.js +160 -181
- package/dist/components/api-key-edit-form.d.ts +4 -3
- package/dist/components/api-key-edit-form.js +33 -27
- package/dist/components/api-key-permissions.d.ts +1 -1
- package/dist/components/api-key-rate-limit.d.ts +1 -1
- package/dist/components/api-key-referrers.d.ts +3 -2
- package/dist/components/api-key-referrers.js +2 -3
- package/dist/components/api-key.d.ts +0 -9
- package/dist/components/api-key.js +0 -39
- package/dist/components/auth-atoms.d.ts +178 -0
- package/dist/components/auth-atoms.js +46 -0
- package/dist/components/auth-client-api.d.ts +1371 -556
- package/dist/components/auth-client-api.js +22 -14
- package/dist/components/auth-forms.d.ts +1 -3
- package/dist/components/auth-forms.js +173 -195
- package/dist/components/auth-provider.d.ts +16 -20
- package/dist/components/auth-provider.js +56 -30
- package/dist/components/has-permission.js +1 -1
- package/dist/components/index.d.ts +19 -20
- package/dist/components/index.js +13 -13
- package/dist/components/member-required.d.ts +1 -3
- package/dist/components/member-required.js +102 -78
- package/dist/components/organization-switcher.d.ts +4 -6
- package/dist/components/organization-switcher.js +539 -390
- package/dist/components/project-access-matrix.d.ts +1 -1
- package/dist/components/project-access-matrix.js +9 -13
- package/dist/components/user-button.d.ts +5 -3
- package/dist/components/user-button.js +291 -336
- package/dist/components/utils.d.ts +3 -1
- package/dist/components/utils.js +4 -11
- package/dist/extra/api.group.d.ts +22 -22
- package/dist/extra/api.group.js +9 -9
- package/dist/extra/schema.d.ts +1 -2
- package/dist/extra/schema.js +2 -3
- package/dist/index.d.ts +11 -12
- package/dist/index.js +11 -12
- package/dist/query.d.ts +2 -2
- package/dist/query.js +5 -5
- package/dist/roles.d.ts +5 -4
- package/dist/roles.js +11 -7
- package/dist/schema.d.ts +1 -1
- package/dist/schema.js +26 -19
- package/dist/server/actor.d.ts +20 -22
- package/dist/server/actor.js +14 -17
- package/dist/server/api.group.d.ts +29 -29
- package/dist/server/api.group.js +14 -14
- package/dist/server/index.d.ts +14 -14
- package/dist/server/index.js +8 -8
- package/dist/server/middleware.d.ts +1 -1
- package/dist/server/middleware.js +1 -1
- package/dist/server/schema.js +1 -1
- package/dist/service.d.ts +1997 -583
- package/dist/service.js +23 -16
- package/package.json +10 -9
- package/dist/better-auth/api.group.d.ts +0 -62
- package/dist/better-auth/api.group.js +0 -39
- package/dist/better-auth/schema.d.ts +0 -72
- package/dist/better-auth/schema.js +0 -110
- package/dist/components/auth-client.d.ts +0 -50
- package/dist/components/auth-client.js +0 -46
|
@@ -2,7 +2,7 @@ import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-run
|
|
|
2
2
|
import { useAtomRefresh, useAtomSet, useAtomValue } from "@effect/atom-react";
|
|
3
3
|
import { FormBuilder, FormReact } from "@lucas-barake/effect-form-react";
|
|
4
4
|
import { useNavigate, useRouterState, } from "@tanstack/react-router";
|
|
5
|
-
import { Effect, Schema } from "effect";
|
|
5
|
+
import { Effect, Option, Predicate, Schema } from "effect";
|
|
6
6
|
import { AsyncResult, Atom } from "effect/unstable/reactivity";
|
|
7
7
|
import { ArrowLeft, Building2, KeyRound, Loader2, LogOutIcon, Pencil, Plus, ShieldCheck, StopCircle, Trash2, UserCircleIcon, Users, UserIcon, } from "lucide-react";
|
|
8
8
|
import { QRCode } from "react-qr-code";
|
|
@@ -16,18 +16,19 @@ import { CopyButton } from "@krak-stack/registry/copy-button";
|
|
|
16
16
|
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "@/components/ui/dialog";
|
|
17
17
|
import { DropdownMenu, DropdownMenuContent, DropdownMenuGroup, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu";
|
|
18
18
|
import { Separator } from "@/components/ui/separator";
|
|
19
|
-
import { authClientApi } from "./auth-client-api";
|
|
20
|
-
import {
|
|
21
|
-
import {
|
|
22
|
-
import {
|
|
23
|
-
import {
|
|
24
|
-
import {
|
|
25
|
-
import {
|
|
26
|
-
import {
|
|
27
|
-
import {
|
|
28
|
-
import {
|
|
29
|
-
import {
|
|
30
|
-
import {
|
|
19
|
+
import { authClientApi, authHttpClient } from "./auth-client-api.js";
|
|
20
|
+
import { authSessionAtom, notifyAuthChange } from "./auth-atoms.js";
|
|
21
|
+
import { useKrakstackAuth } from "./auth-provider.js";
|
|
22
|
+
import { parseApiKeyReferrers } from "./api-key.js";
|
|
23
|
+
import { ApiKeyEditForm } from "./api-key-edit-form.js";
|
|
24
|
+
import { ApiKeyPermissions } from "./api-key-permissions.js";
|
|
25
|
+
import { ApiKeyRateLimit, apiKeyUsagePercent } from "./api-key-rate-limit.js";
|
|
26
|
+
import { ApiKeyReferrers } from "./api-key-referrers.js";
|
|
27
|
+
import { useOpenedOnce } from "./hooks.js";
|
|
28
|
+
import { ExtraUploadedAsset } from "../extra/schema.js";
|
|
29
|
+
import { assetPath, assetUrl } from "./utils.js";
|
|
30
|
+
import { AdminOrganizationsTable } from "./admin-organizations.js";
|
|
31
|
+
import { AdminUsersTable } from "./admin-users.js";
|
|
31
32
|
const messages = {
|
|
32
33
|
en: {
|
|
33
34
|
table_empty: "No results.",
|
|
@@ -106,8 +107,10 @@ const messages = {
|
|
|
106
107
|
user_button_aria_label: "Open user menu",
|
|
107
108
|
user_button_impersonating: "Impersonating",
|
|
108
109
|
user_button_logout: "Log out",
|
|
110
|
+
user_button_logout_error: "Could not log out. Try again.",
|
|
109
111
|
user_button_security: "Security",
|
|
110
112
|
user_button_stop_impersonating: "Stop impersonating",
|
|
113
|
+
user_button_stop_impersonating_error: "Could not stop impersonating. Try again.",
|
|
111
114
|
user_delete: "Delete",
|
|
112
115
|
user_field_password: "Password",
|
|
113
116
|
user_form_description: "Update the profile details associated with your account.",
|
|
@@ -213,8 +216,10 @@ const messages = {
|
|
|
213
216
|
user_button_aria_label: "Ouvrir le menu utilisateur",
|
|
214
217
|
user_button_impersonating: "Imitation",
|
|
215
218
|
user_button_logout: "Se déconnecter",
|
|
219
|
+
user_button_logout_error: "Impossible de se déconnecter. Réessayez.",
|
|
216
220
|
user_button_security: "Sécurité",
|
|
217
221
|
user_button_stop_impersonating: "Arrêter l'imitation",
|
|
222
|
+
user_button_stop_impersonating_error: "Impossible d'arrêter l'imitation. Réessayez.",
|
|
218
223
|
user_delete: "Supprimer",
|
|
219
224
|
user_field_password: "Mot de passe",
|
|
220
225
|
user_form_description: "Mettez à jour les informations de profil associées à votre compte.",
|
|
@@ -258,17 +263,29 @@ const userButtonMessages = (overrides) => ({
|
|
|
258
263
|
const interpolate = (value, params) => params
|
|
259
264
|
? value.replace(/\{([^}]+)\}/g, (_, key) => String(params[key] ?? `{${key}}`))
|
|
260
265
|
: value;
|
|
261
|
-
const
|
|
262
|
-
|
|
263
|
-
|
|
266
|
+
const interpolateMessage = (labels, key, params) => {
|
|
267
|
+
const entry = Object.entries(labels).find(([labelKey]) => labelKey === key);
|
|
268
|
+
return interpolate(entry?.[1] ?? key, params);
|
|
269
|
+
};
|
|
270
|
+
const userButtonMessageFns = (labels) => {
|
|
271
|
+
return new Proxy({}, {
|
|
272
|
+
get: (_target, key) => (params) => interpolateMessage(labels, key, params),
|
|
273
|
+
});
|
|
274
|
+
};
|
|
264
275
|
const UserButtonMessagesContext = createContext(userButtonMessageFns(userButtonMessages()));
|
|
265
276
|
const useUserButtonMessages = () => useContext(UserButtonMessagesContext);
|
|
266
|
-
const
|
|
267
|
-
|
|
268
|
-
const
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
277
|
+
const UserRole = Schema.Struct({ role: Schema.String });
|
|
278
|
+
const hasRole = (role, roles) => {
|
|
279
|
+
const decoded = Schema.decodeUnknownOption(Schema.String)(role);
|
|
280
|
+
return (Option.isSome(decoded) &&
|
|
281
|
+
decoded.value
|
|
282
|
+
.split(",")
|
|
283
|
+
.some((item) => roles.includes(item.trim().toLowerCase())));
|
|
284
|
+
};
|
|
285
|
+
const hasAdminRole = (user) => {
|
|
286
|
+
const decoded = Schema.decodeUnknownOption(UserRole)(user);
|
|
287
|
+
return Option.isSome(decoded) && hasRole(decoded.value.role, ["admin"]);
|
|
288
|
+
};
|
|
272
289
|
const changePasswordFormBuilder = FormBuilder.empty
|
|
273
290
|
.addField("currentPassword", Schema.NonEmptyString)
|
|
274
291
|
.addField("newPassword", Schema.NonEmptyString);
|
|
@@ -281,48 +298,24 @@ const apiKeyFormBuilder = FormBuilder.empty
|
|
|
281
298
|
.addField("name", Schema.NonEmptyString)
|
|
282
299
|
.addField("referrers", Schema.String);
|
|
283
300
|
const isFile = (value) => value instanceof File;
|
|
284
|
-
const
|
|
285
|
-
const uploadUserImageAsset = async (file, m, uploadUserImage) => {
|
|
301
|
+
const uploadUserImageAsset = (file, m, baseUrl) => {
|
|
286
302
|
const payload = new FormData();
|
|
287
303
|
payload.append("file", file);
|
|
288
|
-
|
|
289
|
-
return await decodeUploadedAsset(await uploadUserImage({ payload }));
|
|
290
|
-
}
|
|
291
|
-
catch {
|
|
292
|
-
throw new Error(m.user_profile_image_upload_error());
|
|
293
|
-
}
|
|
304
|
+
return authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.authExtra.uploadUserImage({ payload })), Effect.flatMap(Schema.decodeUnknownEffect(ExtraUploadedAsset)), Effect.mapError(() => new Error(m.user_profile_image_upload_error())));
|
|
294
305
|
};
|
|
295
306
|
const requiresPasswordForAccountRevoke = (accounts) => accounts.some((account) => account.providerId === "credential");
|
|
296
|
-
const userAccountsAtom = Atom.family((
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
})
|
|
306
|
-
const emptyAccountsAtom = Atom.make(Effect.succeed([]));
|
|
307
|
-
const userApiKeysAtom = Atom.family((authClient) => Atom.keepAlive(Atom.make(Effect.tryPromise({
|
|
308
|
-
try: async () => {
|
|
309
|
-
const result = await authClient.apiKey.list({
|
|
310
|
-
query: { configId: "user" },
|
|
311
|
-
});
|
|
312
|
-
if (result.error) {
|
|
313
|
-
throw new Error(result.error.message ?? "Could not load API keys.");
|
|
314
|
-
}
|
|
315
|
-
return result.data?.apiKeys ?? [];
|
|
316
|
-
},
|
|
317
|
-
catch: (error) => error,
|
|
318
|
-
}))));
|
|
319
|
-
const emptyApiKeysAtom = Atom.make(Effect.succeed([]));
|
|
320
|
-
export const UserButton = ({ authClient: providedAuthClient, baseUrl, signOutRedirect = "/", side = "bottom", defaultDialog = null, hideTrigger = false, renderUnauthenticated, menuActions, apiKeyPermissions, messages, dialog: controlledDialog, onDialogChange, }) => {
|
|
307
|
+
const userAccountsAtom = Atom.family((baseUrl) => Atom.keepAlive(Atom.map(authClientApi(baseUrl).query("auth", "listAccounts", {
|
|
308
|
+
reactivityKeys: ["user-accounts", baseUrl],
|
|
309
|
+
}), AsyncResult.map((accounts) => Array.from(accounts)))));
|
|
310
|
+
const emptyAccountsAtom = Atom.make(Effect.succeed(Array()));
|
|
311
|
+
const userApiKeysAtom = Atom.family((baseUrl) => Atom.keepAlive(Atom.map(authClientApi(baseUrl).query("auth", "apiKeyList", {
|
|
312
|
+
query: { configId: "user" },
|
|
313
|
+
reactivityKeys: ["user-api-keys", baseUrl],
|
|
314
|
+
}), AsyncResult.map(({ apiKeys }) => Array.from(apiKeys)))));
|
|
315
|
+
const emptyApiKeysAtom = Atom.make(Effect.succeed(Array()));
|
|
316
|
+
export const UserButton = ({ baseUrl, signOutRedirect = "/", side = "bottom", defaultDialog = null, hideTrigger = false, renderUnauthenticated, menuActions, apiKeyPermissions, messages, dialog: controlledDialog, onDialogChange, }) => {
|
|
321
317
|
const auth = useKrakstackAuth();
|
|
322
|
-
const
|
|
323
|
-
if (!authClient) {
|
|
324
|
-
throw new Error("KrakstackAuthProvider is required to use authClient.");
|
|
325
|
-
}
|
|
318
|
+
const resolvedBaseUrl = baseUrl ?? auth?.baseUrl;
|
|
326
319
|
const labels = userButtonMessages(messages);
|
|
327
320
|
const m = userButtonMessageFns(labels);
|
|
328
321
|
const projectConfig = auth?.projectConfig ?? null;
|
|
@@ -330,19 +323,45 @@ export const UserButton = ({ authClient: providedAuthClient, baseUrl, signOutRed
|
|
|
330
323
|
const currentSiteHref = useRouterState({
|
|
331
324
|
select: (state) => `${import.meta.env.VITE_SITE_URL}${state.location.href}`,
|
|
332
325
|
});
|
|
333
|
-
const
|
|
326
|
+
const sessionAtom = authSessionAtom(resolvedBaseUrl);
|
|
327
|
+
const sessionResult = useAtomValue(sessionAtom);
|
|
328
|
+
const refreshSession = useAtomRefresh(sessionAtom);
|
|
329
|
+
const session = AsyncResult.match(sessionResult, {
|
|
330
|
+
onInitial: () => null,
|
|
331
|
+
onFailure: () => null,
|
|
332
|
+
onSuccess: ({ value }) => value,
|
|
333
|
+
});
|
|
334
|
+
const isPending = sessionResult.waiting;
|
|
334
335
|
const [uncontrolledDialog, setUncontrolledDialog] = useState(defaultDialog);
|
|
335
336
|
const [isStoppingImpersonation, setIsStoppingImpersonation] = useState(false);
|
|
336
337
|
const settingsDialog = controlledDialog !== undefined ? controlledDialog : uncontrolledDialog;
|
|
337
338
|
const [formError, setFormError] = useState(null);
|
|
338
339
|
const shouldLoadAccounts = Boolean(session) &&
|
|
339
340
|
(settingsDialog === "account" || settingsDialog === "security");
|
|
341
|
+
const loadedAccountsAtom = userAccountsAtom(resolvedBaseUrl);
|
|
340
342
|
const accountsAtom = shouldLoadAccounts
|
|
341
|
-
?
|
|
343
|
+
? loadedAccountsAtom
|
|
342
344
|
: emptyAccountsAtom;
|
|
343
345
|
const accountsResult = useAtomValue(accountsAtom);
|
|
344
|
-
const
|
|
345
|
-
const
|
|
346
|
+
const refreshLoadedAccounts = useAtomRefresh(loadedAccountsAtom);
|
|
347
|
+
const refreshEmptyAccounts = useAtomRefresh(emptyAccountsAtom);
|
|
348
|
+
const refreshAccounts = shouldLoadAccounts
|
|
349
|
+
? refreshLoadedAccounts
|
|
350
|
+
: refreshEmptyAccounts;
|
|
351
|
+
const [signOutAction] = useState(() => authClientApi(resolvedBaseUrl).runtime.fn(({ baseUrl, redirectUrl, }) => authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.signOut()), Effect.tap(() => Effect.sync(notifyAuthChange)), Effect.flatMap(() => Effect.tryPromise(() => navigate({ href: redirectUrl }))))));
|
|
352
|
+
const signOut = useAtomSet(signOutAction);
|
|
353
|
+
const signOutResult = useAtomValue(signOutAction);
|
|
354
|
+
const [stopImpersonatingAction] = useState(() => authClientApi(resolvedBaseUrl).runtime.fn((input) => Effect.gen(function* () {
|
|
355
|
+
setIsStoppingImpersonation(true);
|
|
356
|
+
yield* authHttpClient(input.baseUrl).pipe(Effect.flatMap((client) => client.auth.adminStopImpersonating()), Effect.tap(() => Effect.sync(notifyAuthChange)), Effect.tap(() => Effect.sync(input.refreshSession)), Effect.flatMap(() => Effect.tryPromise(() => navigate({ href: input.currentSiteHref }))), Effect.ensuring(Effect.sync(() => setIsStoppingImpersonation(false))));
|
|
357
|
+
})));
|
|
358
|
+
const stopImpersonating = useAtomSet(stopImpersonatingAction);
|
|
359
|
+
const stopImpersonatingResult = useAtomValue(stopImpersonatingAction);
|
|
360
|
+
const authActionError = AsyncResult.isFailure(signOutResult)
|
|
361
|
+
? m.user_button_logout_error()
|
|
362
|
+
: AsyncResult.isFailure(stopImpersonatingResult)
|
|
363
|
+
? m.user_button_stop_impersonating_error()
|
|
364
|
+
: null;
|
|
346
365
|
const accountCache = {
|
|
347
366
|
accounts: AsyncResult.match(accountsResult, {
|
|
348
367
|
onInitial: () => null,
|
|
@@ -357,7 +376,7 @@ export const UserButton = ({ authClient: providedAuthClient, baseUrl, signOutRed
|
|
|
357
376
|
reload: async () => refreshAccounts(),
|
|
358
377
|
};
|
|
359
378
|
const setSettingsDialog = (next) => {
|
|
360
|
-
const nextDialog =
|
|
379
|
+
const nextDialog = Predicate.isFunction(next) ? next(settingsDialog) : next;
|
|
361
380
|
if (nextDialog === settingsDialog)
|
|
362
381
|
return;
|
|
363
382
|
if (controlledDialog === undefined)
|
|
@@ -369,70 +388,54 @@ export const UserButton = ({ authClient: providedAuthClient, baseUrl, signOutRed
|
|
|
369
388
|
}
|
|
370
389
|
const displayName = session.user.name.trim();
|
|
371
390
|
const displayEmail = session.user.email.trim();
|
|
372
|
-
const displayImage = assetUrl(session.user.image,
|
|
391
|
+
const displayImage = assetUrl(session.user.image, resolvedBaseUrl);
|
|
373
392
|
const isImpersonating = Boolean(session.session.impersonatedBy);
|
|
374
393
|
const isOrganizationImpersonating = Boolean(session.session.impersonatedByOrganizationId);
|
|
375
394
|
const canManageUserSettings = !isOrganizationImpersonating;
|
|
376
395
|
const canUseAdminSettings = !isOrganizationImpersonating;
|
|
377
396
|
const isAdmin = hasAdminRole(session.user);
|
|
378
|
-
const
|
|
397
|
+
const startSignOut = () => {
|
|
379
398
|
const redirectUrl = signOutRedirect.startsWith("http")
|
|
380
399
|
? signOutRedirect
|
|
381
400
|
: `${import.meta.env.VITE_SITE_URL}${signOutRedirect.startsWith("/") ? signOutRedirect : `/${signOutRedirect}`}`;
|
|
382
|
-
|
|
383
|
-
await navigate({ href: redirectUrl });
|
|
384
|
-
};
|
|
385
|
-
const stopImpersonating = async () => {
|
|
386
|
-
setIsStoppingImpersonation(true);
|
|
387
|
-
try {
|
|
388
|
-
await authClient.admin.stopImpersonating();
|
|
389
|
-
await refetch();
|
|
390
|
-
await navigate({ href: currentSiteHref });
|
|
391
|
-
}
|
|
392
|
-
finally {
|
|
393
|
-
setIsStoppingImpersonation(false);
|
|
394
|
-
}
|
|
401
|
+
signOut({ baseUrl: resolvedBaseUrl, redirectUrl });
|
|
395
402
|
};
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
const uploaded = await uploadUserImageAsset(imageFile, m, uploadUserImage);
|
|
402
|
-
return assetPath(uploaded.url);
|
|
403
|
-
})()
|
|
404
|
-
: typeof values.image === "string"
|
|
405
|
-
? assetPath(values.image)
|
|
406
|
-
: null;
|
|
407
|
-
const result = await authClient.updateUser({
|
|
408
|
-
name: values.name.trim(),
|
|
409
|
-
image: image?.trim() ?? "",
|
|
410
|
-
});
|
|
411
|
-
if (isAuthErrorResult(result)) {
|
|
412
|
-
throw new Error(result.error.message || m.user_form_update_error());
|
|
413
|
-
}
|
|
414
|
-
await refetch();
|
|
415
|
-
setSettingsDialog(null);
|
|
416
|
-
};
|
|
417
|
-
return (_jsxs(UserButtonMessagesContext.Provider, { value: m, children: [hideTrigger ? null : (_jsxs(DropdownMenu, { children: [_jsx(DropdownMenuTrigger, { render: _jsxs(Button, { variant: "outline", size: "icon", children: [displayImage ? (_jsx("img", { src: displayImage, alt: displayName || displayEmail, className: "size-full rounded-md object-cover" })) : (_jsx(UserIcon, { className: "size-4.5" })), _jsx("span", { className: "sr-only", children: m.user_button_aria_label() })] }) }), _jsx(DropdownMenuContent, { className: "w-56 rounded-lg", side: side, align: "end", sideOffset: 4, children: _jsxs(DropdownMenuGroup, { children: [isImpersonating ? (_jsx("div", { className: "px-1 py-0.5", children: _jsx(Badge, { variant: "secondary", className: "text-xs font-normal", children: m.user_button_impersonating() }) })) : null, _jsx(DropdownMenuLabel, { className: "p-0 font-normal", children: _jsx(AppBrand, { to: null, label: displayName || displayEmail, subtitle: displayName ? displayEmail : m.user_button_account(), icon: UserIcon, className: "px-1 py-1.5 text-left text-sm", ...(displayImage ? { imageSrc: displayImage } : {}) }) }), menuActions ? (_jsxs(_Fragment, { children: [_jsx(DropdownMenuSeparator, {}), menuActions] })) : null, _jsx(DropdownMenuSeparator, {}), canManageUserSettings ? (_jsxs(_Fragment, { children: [_jsxs(DropdownMenuItem, { disabled: isPending, onClick: () => setSettingsDialog("account"), children: [_jsx(UserCircleIcon, {}), m.user_button_account()] }), _jsxs(DropdownMenuItem, { onClick: () => setSettingsDialog("security"), children: [_jsx(ShieldCheck, {}), m.user_button_security()] }), _jsxs(DropdownMenuItem, { onClick: () => setSettingsDialog("apiKeys"), children: [_jsx(KeyRound, {}), m.user_button_api_keys()] }), _jsx(DropdownMenuSeparator, {})] })) : null, (isAdmin && canUseAdminSettings) || isImpersonating ? (_jsxs(_Fragment, { children: [isAdmin && canUseAdminSettings ? (_jsxs(_Fragment, { children: [_jsxs(DropdownMenuItem, { onClick: () => setSettingsDialog("adminUsers"), children: [_jsx(Users, {}), m.user_button_admin_users()] }), _jsxs(DropdownMenuItem, { onClick: () => setSettingsDialog("adminOrganizations"), children: [_jsx(Building2, {}), m.user_button_admin_organizations()] })] })) : null, isImpersonating ? (_jsxs(DropdownMenuItem, { disabled: isStoppingImpersonation, onClick: stopImpersonating, children: [_jsx(StopCircle, {}), m.user_button_stop_impersonating()] })) : null, _jsx(DropdownMenuSeparator, {})] })) : null, _jsxs(DropdownMenuItem, { onClick: signOut, children: [_jsx(LogOutIcon, {}), m.user_button_logout()] })] }) })] })), _jsx(Dialog, { open: canManageUserSettings && settingsDialog === "account", onOpenChange: (open) => {
|
|
403
|
+
return (_jsxs(UserButtonMessagesContext.Provider, { value: m, children: [hideTrigger ? null : (_jsxs(DropdownMenu, { children: [_jsx(DropdownMenuTrigger, { render: _jsxs(Button, { variant: "outline", size: "icon", children: [displayImage ? (_jsx("img", { src: displayImage, alt: displayName || displayEmail, className: "size-full rounded-md object-cover" })) : (_jsx(UserIcon, { className: "size-4.5" })), _jsx("span", { className: "sr-only", children: m.user_button_aria_label() })] }) }), _jsx(DropdownMenuContent, { className: "w-56 rounded-lg", side: side, align: "end", sideOffset: 4, children: _jsxs(DropdownMenuGroup, { children: [isImpersonating ? (_jsx("div", { className: "px-1 py-0.5", children: _jsx(Badge, { variant: "secondary", className: "text-xs font-normal", children: m.user_button_impersonating() }) })) : null, _jsx(DropdownMenuLabel, { className: "p-0 font-normal", children: _jsx(AppBrand, { to: null, label: displayName || displayEmail, subtitle: displayName ? displayEmail : m.user_button_account(), icon: UserIcon, className: "px-1 py-1.5 text-left text-sm", ...(displayImage ? { imageSrc: displayImage } : {}) }) }), menuActions ? (_jsxs(_Fragment, { children: [_jsx(DropdownMenuSeparator, {}), menuActions] })) : null, _jsx(DropdownMenuSeparator, {}), canManageUserSettings ? (_jsxs(_Fragment, { children: [_jsxs(DropdownMenuItem, { disabled: isPending, onClick: () => setSettingsDialog("account"), children: [_jsx(UserCircleIcon, {}), m.user_button_account()] }), _jsxs(DropdownMenuItem, { onClick: () => setSettingsDialog("security"), children: [_jsx(ShieldCheck, {}), m.user_button_security()] }), _jsxs(DropdownMenuItem, { onClick: () => setSettingsDialog("apiKeys"), children: [_jsx(KeyRound, {}), m.user_button_api_keys()] }), _jsx(DropdownMenuSeparator, {})] })) : null, (isAdmin && canUseAdminSettings) || isImpersonating ? (_jsxs(_Fragment, { children: [isAdmin && canUseAdminSettings ? (_jsxs(_Fragment, { children: [_jsxs(DropdownMenuItem, { onClick: () => setSettingsDialog("adminUsers"), children: [_jsx(Users, {}), m.user_button_admin_users()] }), _jsxs(DropdownMenuItem, { onClick: () => setSettingsDialog("adminOrganizations"), children: [_jsx(Building2, {}), m.user_button_admin_organizations()] })] })) : null, isImpersonating ? (_jsxs(DropdownMenuItem, { disabled: isStoppingImpersonation, onClick: () => stopImpersonating({
|
|
404
|
+
baseUrl: resolvedBaseUrl,
|
|
405
|
+
currentSiteHref,
|
|
406
|
+
refreshSession,
|
|
407
|
+
}), children: [_jsx(StopCircle, {}), m.user_button_stop_impersonating()] })) : null, _jsx(DropdownMenuSeparator, {})] })) : null, _jsxs(DropdownMenuItem, { disabled: signOutResult.waiting, onClick: startSignOut, children: [_jsx(LogOutIcon, {}), m.user_button_logout()] }), authActionError ? (_jsx(DropdownMenuLabel, { className: "text-destructive text-xs font-normal", children: authActionError })) : null] }) })] })), _jsx(Dialog, { open: canManageUserSettings && settingsDialog === "account", onOpenChange: (open) => {
|
|
418
408
|
setSettingsDialog((current) => open ? "account" : current === "account" ? null : current);
|
|
419
409
|
}, children: _jsxs(DialogContent, { className: "max-h-[85vh] min-w-0 overflow-x-hidden overflow-y-auto sm:max-w-3xl", children: [_jsxs(DialogHeader, { children: [_jsx(DialogTitle, { className: "text-2xl", children: m.user_form_title() }), _jsx(DialogDescription, { children: m.user_form_description() })] }), _jsx(Separator, {}), isPending ? (_jsx("p", { className: "text-muted-foreground text-sm", children: m.user_loading() })) : (_jsxs("div", { className: "flex flex-col gap-6", children: [_jsx(UserForm, { defaultValues: {
|
|
420
410
|
name: displayName ?? "",
|
|
421
411
|
image: displayImage || null,
|
|
422
|
-
}, error: formError, onSubmit:
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
412
|
+
}, error: formError, onSubmit: (values) => Effect.gen(function* () {
|
|
413
|
+
setFormError(null);
|
|
414
|
+
const imageFile = isFile(values.image)
|
|
415
|
+
? values.image
|
|
416
|
+
: null;
|
|
417
|
+
const image = imageFile
|
|
418
|
+
? assetPath((yield* uploadUserImageAsset(imageFile, m, resolvedBaseUrl)).url)
|
|
419
|
+
: Schema.is(Schema.String)(values.image)
|
|
420
|
+
? assetPath(values.image)
|
|
421
|
+
: null;
|
|
422
|
+
const client = yield* authHttpClient(resolvedBaseUrl);
|
|
423
|
+
yield* client.auth.updateUser({
|
|
424
|
+
payload: {
|
|
425
|
+
name: values.name.trim(),
|
|
426
|
+
image: image?.trim() ?? "",
|
|
427
|
+
},
|
|
428
|
+
});
|
|
429
|
+
notifyAuthChange();
|
|
430
|
+
yield* Effect.sync(refreshSession);
|
|
431
|
+
setSettingsDialog(null);
|
|
432
|
+
}).pipe(Effect.tapError((error) => Effect.sync(() => setFormError(error instanceof Error
|
|
433
|
+
? error.message
|
|
434
|
+
: m.user_form_update_error())))) }), _jsx(Separator, {}), _jsx(ConnectedAccounts, { baseUrl: resolvedBaseUrl, accountCache: accountCache, googleEnabled: projectConfig?.authOptions.google ?? true, currentSiteHref: currentSiteHref, navigate: navigate })] }))] }) }), _jsx(Dialog, { open: canManageUserSettings && settingsDialog === "security", onOpenChange: (open) => {
|
|
432
435
|
setSettingsDialog((current) => open ? "security" : current === "security" ? null : current);
|
|
433
|
-
}, children: _jsxs(DialogContent, { className: "max-h-[85vh] overflow-y-auto sm:max-w-3xl", children: [_jsxs(DialogHeader, { children: [_jsx(DialogTitle, { className: "text-2xl", children: m.user_security_title() }), _jsx(DialogDescription, { children: m.user_security_description() })] }), _jsx(Separator, {}), _jsxs("div", { className: "flex flex-col gap-6", children: [_jsx(PasswordSettings, {
|
|
436
|
+
}, children: _jsxs(DialogContent, { className: "max-h-[85vh] overflow-y-auto sm:max-w-3xl", children: [_jsxs(DialogHeader, { children: [_jsx(DialogTitle, { className: "text-2xl", children: m.user_security_title() }), _jsx(DialogDescription, { children: m.user_security_description() })] }), _jsx(Separator, {}), _jsxs("div", { className: "flex flex-col gap-6", children: [_jsx(PasswordSettings, { baseUrl: resolvedBaseUrl, accountCache: accountCache }), _jsx(Separator, {}), _jsx(AccountSecuritySettings, { baseUrl: resolvedBaseUrl, accountCache: accountCache })] })] }) }), _jsx(Dialog, { open: canManageUserSettings && settingsDialog === "apiKeys", onOpenChange: (open) => {
|
|
434
437
|
setSettingsDialog((current) => open ? "apiKeys" : current === "apiKeys" ? null : current);
|
|
435
|
-
}, children: _jsx(DialogContent, { className: "max-h-[85vh] min-w-0 overflow-x-hidden overflow-y-auto sm:max-w-3xl", children: _jsx(ApiKeyManager, {
|
|
438
|
+
}, children: _jsx(DialogContent, { className: "max-h-[85vh] min-w-0 overflow-x-hidden overflow-y-auto sm:max-w-3xl", children: _jsx(ApiKeyManager, { baseUrl: resolvedBaseUrl, active: settingsDialog === "apiKeys", permissionLabels: auth?.accessLabels ?? undefined, permissions: apiKeyPermissions ?? auth?.access?.apiKeyPermissions.user ?? {} }) }) }), isAdmin ? (_jsxs(_Fragment, { children: [_jsx(Dialog, { open: settingsDialog === "adminUsers", onOpenChange: (open) => {
|
|
436
439
|
setSettingsDialog((current) => open ? "adminUsers" : current === "adminUsers" ? null : current);
|
|
437
440
|
}, children: _jsxs(DialogContent, { className: "flex h-[85vh] flex-col overflow-y-auto sm:max-w-6xl", children: [_jsxs(DialogHeader, { children: [_jsx(DialogTitle, { className: "text-2xl", children: m.user_button_admin_users() }), _jsx(DialogDescription, { children: m.user_button_admin_users_description() })] }), _jsx(Separator, {}), _jsx("div", { className: "min-h-0 flex-1", children: _jsx(AdminUsersTable, {}) })] }) }), _jsx(Dialog, { open: settingsDialog === "adminOrganizations", onOpenChange: (open) => {
|
|
438
441
|
setSettingsDialog((current) => open
|
|
@@ -442,52 +445,55 @@ export const UserButton = ({ authClient: providedAuthClient, baseUrl, signOutRed
|
|
|
442
445
|
: current);
|
|
443
446
|
}, children: _jsxs(DialogContent, { className: "flex h-[85vh] flex-col overflow-y-auto sm:max-w-6xl", children: [_jsxs(DialogHeader, { children: [_jsx(DialogTitle, { className: "text-2xl", children: m.user_button_admin_organizations() }), _jsx(DialogDescription, { children: m.user_button_admin_organizations_description() })] }), _jsx(Separator, {}), _jsx("div", { className: "min-h-0 flex-1", children: _jsx(AdminOrganizationsTable, {}) })] }) })] })) : null] }));
|
|
444
447
|
};
|
|
445
|
-
function ConnectedAccounts({
|
|
448
|
+
function ConnectedAccounts({ baseUrl, accountCache, googleEnabled, currentSiteHref, navigate, }) {
|
|
446
449
|
const m = useUserButtonMessages();
|
|
447
450
|
const [error, setError] = useState(null);
|
|
448
451
|
const [isLinking, setIsLinking] = useState(false);
|
|
449
452
|
const [revokingAccount, setRevokingAccount] = useState(null);
|
|
450
|
-
const
|
|
451
|
-
const googleAccount = accounts.find((account) => account.providerId === "google");
|
|
452
|
-
const requirePassword = requiresPasswordForAccountRevoke(accounts);
|
|
453
|
-
const showGoogleAccount = googleEnabled || Boolean(googleAccount);
|
|
454
|
-
if (accountCache.accounts && (accounts.length === 0 || !showGoogleAccount)) {
|
|
455
|
-
return null;
|
|
456
|
-
}
|
|
457
|
-
const linkGoogle = async () => {
|
|
458
|
-
if (!googleEnabled)
|
|
459
|
-
return;
|
|
453
|
+
const [linkGoogleAction] = useState(() => authClientApi(baseUrl).runtime.fn((input) => Effect.gen(function* () {
|
|
460
454
|
setError(null);
|
|
461
455
|
setIsLinking(true);
|
|
462
|
-
const
|
|
463
|
-
|
|
464
|
-
callbackURL:
|
|
456
|
+
const client = yield* authHttpClient(input.baseUrl);
|
|
457
|
+
const result = yield* client.auth.linkSocial({
|
|
458
|
+
payload: { provider: "google", callbackURL: input.callbackURL },
|
|
465
459
|
});
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
460
|
+
notifyAuthChange();
|
|
461
|
+
yield* Effect.tryPromise(() => accountCache.reload());
|
|
462
|
+
const redirectUrl = result.url;
|
|
463
|
+
if (redirectUrl) {
|
|
464
|
+
yield* Effect.tryPromise(() => navigate({ href: redirectUrl }));
|
|
470
465
|
}
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
return;
|
|
466
|
+
else {
|
|
467
|
+
setIsLinking(false);
|
|
474
468
|
}
|
|
469
|
+
}).pipe(Effect.catch((cause) => Effect.sync(() => {
|
|
470
|
+
setError(cause instanceof Error && cause.message
|
|
471
|
+
? cause.message
|
|
472
|
+
: m.user_account_google_link_error());
|
|
475
473
|
setIsLinking(false);
|
|
476
|
-
};
|
|
477
|
-
const
|
|
478
|
-
|
|
479
|
-
|
|
474
|
+
})))));
|
|
475
|
+
const linkGoogle = useAtomSet(linkGoogleAction);
|
|
476
|
+
const revokeAccount = (account) => authHttpClient(baseUrl).pipe(Effect.tap(() => Effect.sync(() => setError(null))), Effect.flatMap((client) => client.auth.unlinkAccount({
|
|
477
|
+
payload: {
|
|
480
478
|
providerId: account.providerId,
|
|
481
479
|
accountId: account.accountId,
|
|
482
|
-
}
|
|
483
|
-
|
|
484
|
-
|
|
480
|
+
},
|
|
481
|
+
})), Effect.tap(() => Effect.sync(notifyAuthChange)), Effect.flatMap(() => Effect.tryPromise(() => accountCache.reload())), Effect.tap(() => Effect.sync(() => setRevokingAccount(null))), Effect.catch((cause) => Effect.sync(() => setError(cause instanceof Error && cause.message
|
|
482
|
+
? cause.message
|
|
483
|
+
: m.user_account_revoke_error()))));
|
|
484
|
+
const accounts = accountCache.accounts ?? [];
|
|
485
|
+
const googleAccount = accounts.find((account) => account.providerId === "google");
|
|
486
|
+
const requirePassword = requiresPasswordForAccountRevoke(accounts);
|
|
487
|
+
const showGoogleAccount = googleEnabled || Boolean(googleAccount);
|
|
488
|
+
if (accountCache.accounts && (accounts.length === 0 || !showGoogleAccount)) {
|
|
489
|
+
return null;
|
|
490
|
+
}
|
|
491
|
+
const startLinkGoogle = () => {
|
|
492
|
+
if (!googleEnabled)
|
|
485
493
|
return;
|
|
486
|
-
}
|
|
487
|
-
setRevokingAccount(null);
|
|
488
|
-
await accountCache.reload();
|
|
494
|
+
linkGoogle({ baseUrl, callbackURL: currentSiteHref });
|
|
489
495
|
};
|
|
490
|
-
return (_jsxs("section", { className: "flex flex-col gap-4", children: [_jsxs("div", { className: "flex flex-col gap-1.5", children: [_jsx("h2", { className: "text-sm leading-none font-medium", children: m.user_accounts_title() }), _jsx("p", { className: "text-muted-foreground text-sm", children: m.user_accounts_description() })] }), accountCache.accounts ? (_jsxs(_Fragment, { children: [showGoogleAccount ? (_jsx(AccountProviderRow, { icon: _jsx(GoogleLogo, {}), title: m.user_account_google_title(), description: m.user_account_google_description(), account: googleAccount, connected: Boolean(googleAccount), connectLabel: m.user_account_google_connect(), revokeLabel: m.user_account_revoke(), isConnecting: isLinking, renderDisconnected: undefined, onConnect:
|
|
496
|
+
return (_jsxs("section", { className: "flex flex-col gap-4", children: [_jsxs("div", { className: "flex flex-col gap-1.5", children: [_jsx("h2", { className: "text-sm leading-none font-medium", children: m.user_accounts_title() }), _jsx("p", { className: "text-muted-foreground text-sm", children: m.user_accounts_description() })] }), accountCache.accounts ? (_jsxs(_Fragment, { children: [showGoogleAccount ? (_jsx(AccountProviderRow, { icon: _jsx(GoogleLogo, {}), title: m.user_account_google_title(), description: m.user_account_google_description(), account: googleAccount, connected: Boolean(googleAccount), connectLabel: m.user_account_google_connect(), revokeLabel: m.user_account_revoke(), isConnecting: isLinking, renderDisconnected: undefined, onConnect: startLinkGoogle, onRevoke: (account) => setRevokingAccount(account) })) : null, revokingAccount ? (_jsx(RevokeAccountForm, { baseUrl: baseUrl, account: revokingAccount, requirePassword: requirePassword, onCancel: () => setRevokingAccount(null), onRevoke: revokeAccount })) : null] })) : !accountCache.error ? (_jsx("p", { className: "text-muted-foreground text-sm", children: m.user_loading() })) : null, accountCache.error ? (_jsx("p", { className: "text-destructive text-sm", children: accountCache.error })) : null, error ? _jsx("p", { className: "text-destructive text-sm", children: error }) : null] }));
|
|
491
497
|
}
|
|
492
498
|
function AccountProviderRow({ icon, title, description, account, connected, connectLabel, revokeLabel, isConnecting, renderDisconnected, onConnect, onRevoke, }) {
|
|
493
499
|
const m = useUserButtonMessages();
|
|
@@ -498,7 +504,7 @@ function AccountProviderRow({ icon, title, description, account, connected, conn
|
|
|
498
504
|
function GoogleLogo() {
|
|
499
505
|
return (_jsxs("svg", { viewBox: "0 0 24 24", "aria-hidden": "true", className: "size-5", children: [_jsx("path", { fill: "#4285F4", d: "M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z" }), _jsx("path", { fill: "#34A853", d: "M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z" }), _jsx("path", { fill: "#FBBC05", d: "M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z" }), _jsx("path", { fill: "#EA4335", d: "M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84C6.71 7.31 9.14 5.38 12 5.38z" })] }));
|
|
500
506
|
}
|
|
501
|
-
function PasswordSettings({
|
|
507
|
+
function PasswordSettings({ baseUrl, accountCache, }) {
|
|
502
508
|
const [error, setError] = useState(null);
|
|
503
509
|
const [revokingAccount, setRevokingAccount] = useState(null);
|
|
504
510
|
const m = useUserButtonMessages();
|
|
@@ -506,46 +512,34 @@ function PasswordSettings({ authClient, baseUrl, accountCache, }) {
|
|
|
506
512
|
const passwordAccount = accounts.find((account) => account.providerId === "credential");
|
|
507
513
|
const hasPassword = Boolean(passwordAccount);
|
|
508
514
|
const requirePassword = requiresPasswordForAccountRevoke(accounts);
|
|
509
|
-
const revokePassword =
|
|
510
|
-
|
|
511
|
-
const result = await authClient.unlinkAccount({
|
|
515
|
+
const revokePassword = (account) => authHttpClient(baseUrl).pipe(Effect.tap(() => Effect.sync(() => setError(null))), Effect.flatMap((client) => client.auth.unlinkAccount({
|
|
516
|
+
payload: {
|
|
512
517
|
providerId: account.providerId,
|
|
513
518
|
accountId: account.accountId,
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
setRevokingAccount(null);
|
|
520
|
-
await accountCache.reload();
|
|
521
|
-
};
|
|
522
|
-
return (_jsxs("section", { className: "flex flex-col gap-4", children: [_jsxs("div", { className: "flex flex-col gap-1.5", children: [_jsx("h2", { className: "text-sm leading-none font-medium", children: m.user_account_password_title() }), _jsx("p", { className: "text-muted-foreground text-sm", children: m.user_account_password_description() })] }), accountCache.accounts && hasPassword && passwordAccount ? (_jsxs("div", { className: "flex flex-col gap-4", children: [_jsxs("div", { className: "flex flex-wrap items-center justify-between gap-3 rounded-lg border p-4", children: [_jsxs("div", { className: "flex flex-col gap-1", children: [_jsxs("div", { className: "flex flex-wrap items-center gap-2", children: [_jsx("p", { className: "font-medium", children: m.user_account_password_title() }), _jsx(Badge, { children: m.user_account_connected() })] }), _jsx("p", { className: "text-muted-foreground text-sm", children: m.user_account_password_connected_description() })] }), _jsx(Button, { type: "button", variant: "outline", onClick: () => setRevokingAccount(passwordAccount), children: m.user_account_revoke() })] }), revokingAccount ? (_jsx(RevokeAccountForm, { baseUrl: baseUrl, account: revokingAccount, requirePassword: requirePassword, onCancel: () => setRevokingAccount(null), onRevoke: revokePassword })) : null, _jsx(Separator, {}), _jsx(ChangePasswordForm, { authClient: authClient })] })) : accountCache.accounts ? (_jsx(SetPasswordForm, { authClient: authClient, onSaved: accountCache.reload })) : !accountCache.error ? (_jsx("p", { className: "text-muted-foreground text-sm", children: m.user_loading() })) : null, accountCache.error ? (_jsx("p", { className: "text-destructive text-sm", children: accountCache.error })) : null, error ? _jsx("p", { className: "text-destructive text-sm", children: error }) : null] }));
|
|
519
|
+
},
|
|
520
|
+
})), Effect.tap(() => Effect.sync(notifyAuthChange)), Effect.flatMap(() => Effect.tryPromise(() => accountCache.reload())), Effect.tap(() => Effect.sync(() => setRevokingAccount(null))), Effect.catch((cause) => Effect.sync(() => setError(cause instanceof Error && cause.message
|
|
521
|
+
? cause.message
|
|
522
|
+
: m.user_account_revoke_error()))));
|
|
523
|
+
return (_jsxs("section", { className: "flex flex-col gap-4", children: [_jsxs("div", { className: "flex flex-col gap-1.5", children: [_jsx("h2", { className: "text-sm leading-none font-medium", children: m.user_account_password_title() }), _jsx("p", { className: "text-muted-foreground text-sm", children: m.user_account_password_description() })] }), accountCache.accounts && hasPassword && passwordAccount ? (_jsxs("div", { className: "flex flex-col gap-4", children: [_jsxs("div", { className: "flex flex-wrap items-center justify-between gap-3 rounded-lg border p-4", children: [_jsxs("div", { className: "flex flex-col gap-1", children: [_jsxs("div", { className: "flex flex-wrap items-center gap-2", children: [_jsx("p", { className: "font-medium", children: m.user_account_password_title() }), _jsx(Badge, { children: m.user_account_connected() })] }), _jsx("p", { className: "text-muted-foreground text-sm", children: m.user_account_password_connected_description() })] }), _jsx(Button, { type: "button", variant: "outline", onClick: () => setRevokingAccount(passwordAccount), children: m.user_account_revoke() })] }), revokingAccount ? (_jsx(RevokeAccountForm, { baseUrl: baseUrl, account: revokingAccount, requirePassword: requirePassword, onCancel: () => setRevokingAccount(null), onRevoke: revokePassword })) : null, _jsx(Separator, {}), _jsx(ChangePasswordForm, { baseUrl: baseUrl })] })) : accountCache.accounts ? (_jsx(SetPasswordForm, { baseUrl: baseUrl, onSaved: accountCache.reload })) : !accountCache.error ? (_jsx("p", { className: "text-muted-foreground text-sm", children: m.user_loading() })) : null, accountCache.error ? (_jsx("p", { className: "text-destructive text-sm", children: accountCache.error })) : null, error ? _jsx("p", { className: "text-destructive text-sm", children: error }) : null] }));
|
|
523
524
|
}
|
|
524
|
-
function ChangePasswordForm({
|
|
525
|
+
function ChangePasswordForm({ baseUrl }) {
|
|
525
526
|
const m = useUserButtonMessages();
|
|
526
527
|
const [saved, setSaved] = useState(false);
|
|
528
|
+
let reset = () => { };
|
|
527
529
|
const [form] = useState(() => FormReact.make(changePasswordFormBuilder, {
|
|
530
|
+
runtime: authClientApi(baseUrl).runtime,
|
|
528
531
|
fields: { currentPassword: TextField, newPassword: TextField },
|
|
529
532
|
mode: { validation: "onSubmit" },
|
|
530
|
-
onSubmit: (_, { decoded }) => Effect.
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
currentPassword: decoded.currentPassword,
|
|
535
|
-
newPassword: decoded.newPassword,
|
|
536
|
-
});
|
|
537
|
-
if (result.error) {
|
|
538
|
-
throw new Error(result.error.message ?? m.user_account_password_change_error());
|
|
539
|
-
}
|
|
540
|
-
reset();
|
|
541
|
-
setSaved(true);
|
|
533
|
+
onSubmit: (_, { decoded }) => authHttpClient(baseUrl).pipe(Effect.tap(() => Effect.sync(() => setSaved(false))), Effect.flatMap((client) => client.auth.changePassword({
|
|
534
|
+
payload: {
|
|
535
|
+
currentPassword: decoded.currentPassword,
|
|
536
|
+
newPassword: decoded.newPassword,
|
|
542
537
|
},
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
}),
|
|
538
|
+
})), Effect.tap(() => Effect.sync(notifyAuthChange)), Effect.tap(() => Effect.sync(() => reset())), Effect.tap(() => Effect.sync(() => setSaved(true))), Effect.mapError((cause) => cause instanceof Error
|
|
539
|
+
? cause
|
|
540
|
+
: new Error(m.user_account_password_change_error()))),
|
|
547
541
|
}));
|
|
548
|
-
|
|
542
|
+
reset = useAtomSet(form.reset);
|
|
549
543
|
const submit = useAtomSet(form.submit);
|
|
550
544
|
const submitResult = useAtomValue(form.submit);
|
|
551
545
|
return (_jsx(form.Initialize, { defaultValues: { currentPassword: "", newPassword: "" }, children: _jsxs("form", { className: "flex w-full flex-col gap-3 sm:max-w-md", onSubmit: (event) => {
|
|
@@ -554,29 +548,20 @@ function ChangePasswordForm({ authClient }) {
|
|
|
554
548
|
submit();
|
|
555
549
|
}, children: [_jsxs("div", { className: "flex flex-col gap-1", children: [_jsx("h3", { className: "text-sm font-medium", children: m.user_account_password_change_title() }), _jsx("p", { className: "text-muted-foreground text-sm", children: m.user_account_password_change_description() })] }), _jsx(form.currentPassword, { label: m.user_account_current_password(), type: "password", autoComplete: "current-password", required: true }), _jsx(form.newPassword, { label: m.user_account_new_password(), type: "password", autoComplete: "new-password", required: true }), _jsx(SubmitError, { result: submitResult }), saved ? (_jsx("p", { className: "text-sm text-green-600", children: m.user_account_password_change_success() })) : null, _jsx(SubmitButton, { form: form, children: m.user_account_password_change_submit() })] }) }));
|
|
556
550
|
}
|
|
557
|
-
function SetPasswordForm({
|
|
551
|
+
function SetPasswordForm({ baseUrl, onSaved, }) {
|
|
558
552
|
const m = useUserButtonMessages();
|
|
553
|
+
let reset = () => { };
|
|
559
554
|
const [form] = useState(() => FormReact.make(passwordFormBuilder(true), {
|
|
555
|
+
runtime: authClientApi(baseUrl).runtime,
|
|
560
556
|
fields: { password: TextField },
|
|
561
557
|
mode: { validation: "onSubmit" },
|
|
562
|
-
onSubmit: (_, { decoded }) => Effect.
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
});
|
|
568
|
-
if (result.error) {
|
|
569
|
-
throw new Error(result.error.message ?? m.user_account_password_set_error());
|
|
570
|
-
}
|
|
571
|
-
reset();
|
|
572
|
-
await onSaved();
|
|
573
|
-
},
|
|
574
|
-
catch: (cause) => cause instanceof Error
|
|
575
|
-
? cause
|
|
576
|
-
: new Error(m.user_account_password_set_error()),
|
|
577
|
-
}),
|
|
558
|
+
onSubmit: (_, { decoded }) => authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.authExtra.setPassword({
|
|
559
|
+
payload: { newPassword: decoded.password },
|
|
560
|
+
})), Effect.tap(() => Effect.sync(notifyAuthChange)), Effect.tap(() => Effect.sync(() => reset())), Effect.flatMap(() => Effect.tryPromise(() => onSaved())), Effect.mapError((cause) => cause instanceof Error
|
|
561
|
+
? cause
|
|
562
|
+
: new Error(m.user_account_password_set_error()))),
|
|
578
563
|
}));
|
|
579
|
-
|
|
564
|
+
reset = useAtomSet(form.reset);
|
|
580
565
|
const submit = useAtomSet(form.submit);
|
|
581
566
|
const submitResult = useAtomValue(form.submit);
|
|
582
567
|
return (_jsx(form.Initialize, { defaultValues: { password: "" }, children: _jsxs("form", { className: "flex w-full flex-col gap-3 sm:max-w-sm", onSubmit: (event) => {
|
|
@@ -587,23 +572,21 @@ function SetPasswordForm({ authClient, onSaved, }) {
|
|
|
587
572
|
}
|
|
588
573
|
function RevokeAccountForm({ baseUrl, account, requirePassword, onCancel, onRevoke, }) {
|
|
589
574
|
const m = useUserButtonMessages();
|
|
590
|
-
const verifyPassword = useAtomSet(authClientApi(baseUrl).mutation("authExtra", "verifyPassword"), { mode: "promise" });
|
|
591
575
|
const [form] = useState(() => FormReact.make(passwordFormBuilder(requirePassword), {
|
|
576
|
+
runtime: authClientApi(baseUrl).runtime,
|
|
592
577
|
fields: { password: TextField },
|
|
593
578
|
mode: { validation: "onSubmit" },
|
|
594
|
-
onSubmit: (_, { decoded }) => Effect.
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
: new Error(m.user_account_password_verify_error()),
|
|
606
|
-
}),
|
|
579
|
+
onSubmit: (_, { decoded }) => Effect.gen(function* () {
|
|
580
|
+
if (requirePassword) {
|
|
581
|
+
const client = yield* authHttpClient(baseUrl);
|
|
582
|
+
yield* client.authExtra.verifyPassword({
|
|
583
|
+
payload: { password: decoded.password },
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
yield* onRevoke(account);
|
|
587
|
+
}).pipe(Effect.mapError((cause) => cause instanceof Error
|
|
588
|
+
? cause
|
|
589
|
+
: new Error(m.user_account_password_verify_error()))),
|
|
607
590
|
}));
|
|
608
591
|
const submit = useAtomSet(form.submit);
|
|
609
592
|
const submitResult = useAtomValue(form.submit);
|
|
@@ -627,12 +610,9 @@ const UserForm = ({ defaultValues, error, onSubmit, }) => {
|
|
|
627
610
|
const [form] = useState(() => FormReact.make(userFormBuilder, {
|
|
628
611
|
fields: { name: TextField, image: ImageField },
|
|
629
612
|
mode: { validation: "onSubmit" },
|
|
630
|
-
onSubmit: (_, { decoded }) => Effect.
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
? cause
|
|
634
|
-
: new Error(m.user_form_update_error()),
|
|
635
|
-
}),
|
|
613
|
+
onSubmit: (_, { decoded }) => onSubmit({ ...decoded, image: decoded.image ?? null }).pipe(Effect.mapError((cause) => cause instanceof Error
|
|
614
|
+
? cause
|
|
615
|
+
: new Error(m.user_form_update_error()))),
|
|
636
616
|
}));
|
|
637
617
|
const submit = useAtomSet(form.submit);
|
|
638
618
|
return (_jsxs("section", { className: "flex flex-col gap-4", children: [_jsxs("div", { className: "flex flex-col gap-1.5", children: [_jsx("h2", { className: "text-sm leading-none font-medium", children: m.user_profile_title() }), _jsx("p", { className: "text-muted-foreground text-sm", children: m.user_profile_description() })] }), _jsx(form.Initialize, { defaultValues: defaultValues, children: _jsxs("form", { className: "flex flex-col gap-4", onSubmit: (event) => {
|
|
@@ -646,12 +626,19 @@ const UserForm = ({ defaultValues, error, onSubmit, }) => {
|
|
|
646
626
|
suggestedHeight: 512,
|
|
647
627
|
} }), _jsx(form.name, { label: m.user_form_name_label(), autoComplete: "name", required: true }), error && (_jsx("p", { role: "alert", className: "text-destructive text-sm", children: error })), _jsx(SubmitButton, { form: form })] }) })] }));
|
|
648
628
|
};
|
|
649
|
-
function AccountSecuritySettings({
|
|
629
|
+
function AccountSecuritySettings({ baseUrl, accountCache, }) {
|
|
650
630
|
const m = useUserButtonMessages();
|
|
651
|
-
const
|
|
631
|
+
const sessionAtom = authSessionAtom(baseUrl);
|
|
632
|
+
const sessionResult = useAtomValue(sessionAtom);
|
|
633
|
+
const refreshSession = useAtomRefresh(sessionAtom);
|
|
634
|
+
const session = AsyncResult.match(sessionResult, {
|
|
635
|
+
onInitial: () => null,
|
|
636
|
+
onFailure: () => null,
|
|
637
|
+
onSuccess: ({ value }) => value,
|
|
638
|
+
});
|
|
652
639
|
const [setup, setSetup] = useState(null);
|
|
653
640
|
const [message, setMessage] = useState(null);
|
|
654
|
-
const twoFactorEnabled = hasTwoFactorEnabled(session
|
|
641
|
+
const twoFactorEnabled = hasTwoFactorEnabled(session?.user);
|
|
655
642
|
const requirePassword = accountCache.accounts
|
|
656
643
|
? accountCache.accounts.some((account) => account.providerId === "credential")
|
|
657
644
|
: null;
|
|
@@ -659,35 +646,29 @@ function AccountSecuritySettings({ authClient, accountCache, }) {
|
|
|
659
646
|
? m.user_two_factor_enabled()
|
|
660
647
|
: m.user_two_factor_disabled() })] }), requirePassword === null ? (_jsx("p", { className: accountCache.error
|
|
661
648
|
? "text-destructive text-sm"
|
|
662
|
-
: "text-muted-foreground text-sm", children: accountCache.error ?? m.user_loading() })) : twoFactorEnabled ? (_jsx(DisableTotpForm, {
|
|
649
|
+
: "text-muted-foreground text-sm", children: accountCache.error ?? m.user_loading() })) : twoFactorEnabled ? (_jsx(DisableTotpForm, { baseUrl: baseUrl, requirePassword: requirePassword, onDisabled: async () => {
|
|
663
650
|
setMessage(m.user_two_factor_disabled_message());
|
|
664
|
-
await
|
|
665
|
-
} })) : setup ? (_jsx(VerifyTotpSetup, {
|
|
651
|
+
await refreshSession();
|
|
652
|
+
} })) : setup ? (_jsx(VerifyTotpSetup, { baseUrl: baseUrl, setup: setup, onVerified: async () => {
|
|
666
653
|
setSetup(null);
|
|
667
654
|
setMessage(m.user_two_factor_enabled_message());
|
|
668
|
-
await
|
|
669
|
-
} })) : (_jsx(EnableTotpForm, {
|
|
655
|
+
await refreshSession();
|
|
656
|
+
} })) : (_jsx(EnableTotpForm, { baseUrl: baseUrl, requirePassword: requirePassword, onEnabled: setSetup })), message ? (_jsx("p", { className: "text-muted-foreground text-sm", children: message })) : null] }));
|
|
670
657
|
}
|
|
671
|
-
function EnableTotpForm({
|
|
658
|
+
function EnableTotpForm({ baseUrl, requirePassword, onEnabled, }) {
|
|
672
659
|
const m = useUserButtonMessages();
|
|
673
660
|
const [form] = useState(() => FormReact.make(passwordFormBuilder(requirePassword), {
|
|
661
|
+
runtime: authClientApi(baseUrl).runtime,
|
|
674
662
|
fields: { password: TextField },
|
|
675
663
|
mode: { validation: "onSubmit" },
|
|
676
|
-
onSubmit: (_, { decoded }) => Effect.
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
backupCodes: result.data.backupCodes,
|
|
685
|
-
});
|
|
686
|
-
},
|
|
687
|
-
catch: (cause) => cause instanceof Error
|
|
688
|
-
? cause
|
|
689
|
-
: new Error(m.user_two_factor_enable_error()),
|
|
690
|
-
}),
|
|
664
|
+
onSubmit: (_, { decoded }) => authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.twoFactorEnable({
|
|
665
|
+
payload: requirePassword ? { password: decoded.password } : {},
|
|
666
|
+
})), Effect.tap(() => Effect.sync(notifyAuthChange)), Effect.tap((result) => Effect.sync(() => onEnabled({
|
|
667
|
+
totpURI: result.totpURI,
|
|
668
|
+
backupCodes: Array.from(result.backupCodes),
|
|
669
|
+
}))), Effect.mapError((cause) => cause instanceof Error
|
|
670
|
+
? cause
|
|
671
|
+
: new Error(m.user_two_factor_enable_error()))),
|
|
691
672
|
}));
|
|
692
673
|
const submit = useAtomSet(form.submit);
|
|
693
674
|
const submitResult = useAtomValue(form.submit);
|
|
@@ -697,25 +678,17 @@ function EnableTotpForm({ authClient, requirePassword, onEnabled, }) {
|
|
|
697
678
|
submit();
|
|
698
679
|
}, children: [requirePassword ? (_jsx(form.password, { label: m.user_field_password(), type: "password", autoComplete: "current-password", required: true })) : null, _jsx(SubmitError, { result: submitResult }), requirePassword ? (_jsx(SubmitButton, { form: form, children: m.user_two_factor_enable_submit() })) : (_jsxs(Button, { type: "button", disabled: submitResult.waiting, onClick: () => submit(), children: [submitResult.waiting ? (_jsx(Loader2, { "data-icon": "inline-start", className: "animate-spin" })) : null, m.user_two_factor_enable_submit()] }))] }) }));
|
|
699
680
|
}
|
|
700
|
-
function VerifyTotpSetup({
|
|
681
|
+
function VerifyTotpSetup({ baseUrl, setup, onVerified, }) {
|
|
701
682
|
const m = useUserButtonMessages();
|
|
702
683
|
const [form] = useState(() => FormReact.make(totpCodeFormBuilder, {
|
|
684
|
+
runtime: authClientApi(baseUrl).runtime,
|
|
703
685
|
fields: { code: TextField },
|
|
704
686
|
mode: { validation: "onSubmit" },
|
|
705
|
-
onSubmit: (_, { decoded }) => Effect.
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
if (result.error) {
|
|
711
|
-
throw new Error(result.error.message ?? m.user_two_factor_verify_error());
|
|
712
|
-
}
|
|
713
|
-
await onVerified();
|
|
714
|
-
},
|
|
715
|
-
catch: (cause) => cause instanceof Error
|
|
716
|
-
? cause
|
|
717
|
-
: new Error(m.user_two_factor_verify_error()),
|
|
718
|
-
}),
|
|
687
|
+
onSubmit: (_, { decoded }) => authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.twoFactorVerifyTotp({
|
|
688
|
+
payload: { code: decoded.code.trim() },
|
|
689
|
+
})), Effect.tap(() => Effect.sync(notifyAuthChange)), Effect.flatMap(() => Effect.tryPromise(() => onVerified())), Effect.mapError((cause) => cause instanceof Error
|
|
690
|
+
? cause
|
|
691
|
+
: new Error(m.user_two_factor_verify_error()))),
|
|
719
692
|
}));
|
|
720
693
|
const submit = useAtomSet(form.submit);
|
|
721
694
|
const submitResult = useAtomValue(form.submit);
|
|
@@ -725,23 +698,17 @@ function VerifyTotpSetup({ authClient, setup, onVerified, }) {
|
|
|
725
698
|
submit();
|
|
726
699
|
}, children: [_jsx(form.code, { label: m.user_two_factor_code(), autoComplete: "one-time-code", inputMode: "numeric", required: true }), _jsx(SubmitError, { result: submitResult }), _jsx(SubmitButton, { form: form })] }) })] }));
|
|
727
700
|
}
|
|
728
|
-
function DisableTotpForm({
|
|
701
|
+
function DisableTotpForm({ baseUrl, requirePassword, onDisabled, }) {
|
|
729
702
|
const m = useUserButtonMessages();
|
|
730
703
|
const [form] = useState(() => FormReact.make(passwordFormBuilder(requirePassword), {
|
|
704
|
+
runtime: authClientApi(baseUrl).runtime,
|
|
731
705
|
fields: { password: TextField },
|
|
732
706
|
mode: { validation: "onSubmit" },
|
|
733
|
-
onSubmit: (_, { decoded }) => Effect.
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
}
|
|
739
|
-
await onDisabled();
|
|
740
|
-
},
|
|
741
|
-
catch: (cause) => cause instanceof Error
|
|
742
|
-
? cause
|
|
743
|
-
: new Error(m.user_two_factor_disable_error()),
|
|
744
|
-
}),
|
|
707
|
+
onSubmit: (_, { decoded }) => authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.twoFactorDisable({
|
|
708
|
+
payload: requirePassword ? { password: decoded.password } : {},
|
|
709
|
+
})), Effect.tap(() => Effect.sync(notifyAuthChange)), Effect.flatMap(() => Effect.tryPromise(() => onDisabled())), Effect.mapError((cause) => cause instanceof Error
|
|
710
|
+
? cause
|
|
711
|
+
: new Error(m.user_two_factor_disable_error()))),
|
|
745
712
|
}));
|
|
746
713
|
const submit = useAtomSet(form.submit);
|
|
747
714
|
const submitResult = useAtomValue(form.submit);
|
|
@@ -751,12 +718,15 @@ function DisableTotpForm({ authClient, requirePassword, onDisabled, }) {
|
|
|
751
718
|
submit();
|
|
752
719
|
}, children: [requirePassword ? (_jsx(form.password, { label: m.user_field_password(), type: "password", autoComplete: "current-password", required: true })) : null, _jsx(SubmitError, { result: submitResult }), requirePassword ? (_jsx(SubmitButton, { form: form })) : (_jsxs(Button, { type: "button", disabled: submitResult.waiting, onClick: () => submit(), children: [submitResult.waiting ? (_jsx(Loader2, { "data-icon": "inline-start", className: "animate-spin" })) : null, effectFormMessages().submit] }))] }) }));
|
|
753
720
|
}
|
|
754
|
-
function ApiKeyManager({
|
|
721
|
+
function ApiKeyManager({ baseUrl, active, permissionLabels, permissions = {}, }) {
|
|
755
722
|
const m = useUserButtonMessages();
|
|
756
723
|
const hasOpened = useOpenedOnce(active);
|
|
757
|
-
const
|
|
724
|
+
const loadedKeysAtom = userApiKeysAtom(baseUrl);
|
|
725
|
+
const keysAtom = hasOpened ? loadedKeysAtom : emptyApiKeysAtom;
|
|
758
726
|
const keysResult = useAtomValue(keysAtom);
|
|
759
|
-
const
|
|
727
|
+
const refreshLoadedKeys = useAtomRefresh(loadedKeysAtom);
|
|
728
|
+
const refreshEmptyKeys = useAtomRefresh(emptyApiKeysAtom);
|
|
729
|
+
const refreshKeys = hasOpened ? refreshLoadedKeys : refreshEmptyKeys;
|
|
760
730
|
const [createdKey, setCreatedKey] = useState(null);
|
|
761
731
|
const [creating, setCreating] = useState(false);
|
|
762
732
|
const [editingKey, setEditingKey] = useState(null);
|
|
@@ -779,26 +749,32 @@ function ApiKeyManager({ authClient, active, permissionLabels, permissions = {},
|
|
|
779
749
|
permissionOptionsRef.current = permissionOptions;
|
|
780
750
|
selectedPermissionsRef.current = selectedPermissions;
|
|
781
751
|
const [createForm] = useState(() => FormReact.make(apiKeyFormBuilder, {
|
|
752
|
+
runtime: authClientApi(baseUrl).runtime,
|
|
782
753
|
fields: { name: TextField, referrers: TextAreaField },
|
|
783
754
|
mode: { validation: "onSubmit" },
|
|
784
|
-
onSubmit: (_, { decoded }) => Effect.
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
755
|
+
onSubmit: (_, { decoded }) => Effect.gen(function* () {
|
|
756
|
+
setCreatedKey(null);
|
|
757
|
+
const referrers = yield* Effect.try({
|
|
758
|
+
try: () => parseApiKeyReferrers(decoded.referrers, m.user_api_key_referrers_error()),
|
|
759
|
+
catch: (cause) => cause,
|
|
760
|
+
});
|
|
761
|
+
const client = yield* authHttpClient(baseUrl);
|
|
762
|
+
return yield* client.authExtra.createApiKey({
|
|
763
|
+
payload: {
|
|
788
764
|
configId: "user",
|
|
789
765
|
name: decoded.name.trim(),
|
|
790
766
|
permissions: getSelectedPermissionObject(permissionOptionsRef.current, selectedPermissionsRef.current),
|
|
791
|
-
referrers
|
|
792
|
-
},
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
}
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
767
|
+
referrers,
|
|
768
|
+
},
|
|
769
|
+
});
|
|
770
|
+
}).pipe(Effect.tap((created) => Effect.sync(() => {
|
|
771
|
+
setCreatedKey(created.key);
|
|
772
|
+
resetCreateForm();
|
|
773
|
+
setSelectedPermissions({});
|
|
774
|
+
refreshKeys();
|
|
775
|
+
})), Effect.mapError((cause) => cause instanceof Error
|
|
776
|
+
? cause
|
|
777
|
+
: new Error(m.user_api_key_create_error()))),
|
|
802
778
|
}));
|
|
803
779
|
const resetCreateForm = useAtomSet(createForm.reset);
|
|
804
780
|
const submitCreateForm = useAtomSet(createForm.submit);
|
|
@@ -806,17 +782,12 @@ function ApiKeyManager({ authClient, active, permissionLabels, permissions = {},
|
|
|
806
782
|
const togglePermission = (id, checked) => {
|
|
807
783
|
setSelectedPermissions((current) => ({ ...current, [id]: checked }));
|
|
808
784
|
};
|
|
809
|
-
const
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
setError(result.error.message ?? m.user_api_key_delete_error());
|
|
816
|
-
return;
|
|
817
|
-
}
|
|
818
|
-
refreshKeys();
|
|
819
|
-
};
|
|
785
|
+
const [deleteKeyAction] = useState(() => authClientApi(baseUrl).runtime.fn((key) => authHttpClient(baseUrl).pipe(Effect.flatMap((client) => client.auth.apiKeyDelete({
|
|
786
|
+
payload: { configId: "user", keyId: key.id },
|
|
787
|
+
})), Effect.tap(() => Effect.sync(refreshKeys)), Effect.catch((cause) => Effect.sync(() => setError(cause instanceof Error && cause.message
|
|
788
|
+
? cause.message
|
|
789
|
+
: m.user_api_key_delete_error()))))));
|
|
790
|
+
const deleteKey = useAtomSet(deleteKeyAction);
|
|
820
791
|
return (_jsxs(_Fragment, { children: [_jsxs(DialogHeader, { children: [_jsx(DialogTitle, { className: "text-2xl", children: editingKey
|
|
821
792
|
? m.user_api_key_edit_title()
|
|
822
793
|
: createdKey
|
|
@@ -843,7 +814,7 @@ function ApiKeyManager({ authClient, active, permissionLabels, permissions = {},
|
|
|
843
814
|
}, children: [_jsx(createForm.name, { label: m.user_api_key_name(), required: true }), _jsx(createForm.referrers, { label: m.user_api_key_referrers(), description: m.user_api_key_referrers_description(), placeholder: m.user_api_key_referrers_placeholder(), rows: 3 }), _jsx(ApiKeyPermissions, { description: m.user_api_key_permissions_description(), idPrefix: "user-create", permissions: permissions, labels: permissionLabels, selected: selectedPermissions, title: m.user_api_key_permissions(), onChange: togglePermission }), _jsx(SubmitError, { result: createSubmitResult }), _jsx(SubmitButton, { form: createForm })] }) }) })) : null, creating && createdKey ? (_jsx("div", { className: "flex min-w-0 flex-col gap-3", children: _jsxs("div", { className: "bg-muted/50 flex min-w-0 items-center justify-between gap-3 rounded-md border px-3 py-2", children: [_jsx("code", { className: "block min-w-0 flex-1 overflow-x-auto text-sm whitespace-nowrap", children: createdKey }), _jsx(CopyButton, { className: "shrink-0", messages: {
|
|
844
815
|
copied: m.user_api_key_copied(),
|
|
845
816
|
copy: m.user_api_key_copy(),
|
|
846
|
-
}, value: createdKey, variant: "ghost" })] }) })) : null, !creating && !editingKey && keysError ? (_jsx("p", { className: "text-destructive text-sm", children: keysError })) : null, !creating && !editingKey && error ? (_jsx("p", { className: "text-destructive text-sm", children: error })) : null, !creating && !editingKey ? (_jsx("div", { className: "max-w-full min-w-0 overflow-x-hidden", children: _jsx(DataTable, {
|
|
817
|
+
}, value: createdKey, variant: "ghost" })] }) })) : null, !creating && !editingKey && keysError ? (_jsx("p", { className: "text-destructive text-sm", children: keysError })) : null, !creating && !editingKey && error ? (_jsx("p", { className: "text-destructive text-sm", children: error })) : null, !creating && !editingKey ? (_jsx("div", { className: "max-w-full min-w-0 overflow-x-hidden", children: _jsx(DataTable, { columnDefs: apiKeyColumns({ m }), rowData: keys, features: {
|
|
847
818
|
export: { baseName: m.user_api_keys_export_file_name() },
|
|
848
819
|
gallery: false,
|
|
849
820
|
rowActions: {
|
|
@@ -853,10 +824,10 @@ function ApiKeyManager({ authClient, active, permissionLabels, permissions = {},
|
|
|
853
824
|
onEdit: (key) => setEditingKey(key),
|
|
854
825
|
}),
|
|
855
826
|
},
|
|
856
|
-
},
|
|
827
|
+
}, status: {
|
|
857
828
|
empty: loading ? m.user_loading() : m.table_empty(),
|
|
858
829
|
loading,
|
|
859
|
-
} }) })) : null, editingKey ? (_jsx(ApiKeyEditForm, { configId: "user", keyData: editingKey, permissions: permissions, permissionLabels: permissionLabels, messages: {
|
|
830
|
+
} }) })) : null, editingKey ? (_jsx(ApiKeyEditForm, { baseUrl: baseUrl, configId: "user", keyData: editingKey, permissions: permissions, permissionLabels: permissionLabels, messages: {
|
|
860
831
|
enabled: m.user_api_key_enabled(),
|
|
861
832
|
name: m.user_api_key_name(),
|
|
862
833
|
referrers: m.user_api_key_referrers(),
|
|
@@ -873,28 +844,26 @@ function ApiKeyManager({ authClient, active, permissionLabels, permissions = {},
|
|
|
873
844
|
}
|
|
874
845
|
const apiKeyColumns = ({ m, }) => [
|
|
875
846
|
{
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
847
|
+
field: "name",
|
|
848
|
+
headerName: m.user_api_key_name(),
|
|
849
|
+
cellRenderer: ({ data }) => (_jsx("div", { className: "min-w-0", children: _jsx("p", { className: "truncate font-medium", children: data.name ?? m.user_api_key_hidden() }) })),
|
|
879
850
|
},
|
|
880
851
|
{
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
? m.user_api_key_enabled()
|
|
885
|
-
: m.user_api_key_disabled() })),
|
|
852
|
+
field: "enabled",
|
|
853
|
+
headerName: m.user_api_key_status(),
|
|
854
|
+
cellRenderer: ({ data }) => (_jsx(Badge, { variant: data.enabled ? "default" : "secondary", children: data.enabled ? m.user_api_key_enabled() : m.user_api_key_disabled() })),
|
|
886
855
|
},
|
|
887
856
|
{
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
857
|
+
colId: "referrers",
|
|
858
|
+
valueGetter: ({ data }) => data.metadata,
|
|
859
|
+
headerName: m.user_api_key_referrers_column(),
|
|
860
|
+
cellRenderer: ({ data }) => (_jsx(ApiKeyReferrers, { metadata: data.metadata, unrestrictedLabel: m.user_api_key_referrers_any() })),
|
|
892
861
|
},
|
|
893
862
|
{
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
863
|
+
colId: "rateLimit",
|
|
864
|
+
valueGetter: ({ data }) => apiKeyUsagePercent(data),
|
|
865
|
+
headerName: m.user_api_key_rate_limit(),
|
|
866
|
+
cellRenderer: ({ data }) => (_jsx(ApiKeyRateLimit, { keyData: data, messages: {
|
|
898
867
|
disabled: m.user_api_key_disabled(),
|
|
899
868
|
none: m.user_api_key_window_none(),
|
|
900
869
|
unlimited: m.user_api_key_unlimited(),
|
|
@@ -906,10 +875,10 @@ const apiKeyColumns = ({ m, }) => [
|
|
|
906
875
|
} })),
|
|
907
876
|
},
|
|
908
877
|
{
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
const permissions = formatPermissions(
|
|
878
|
+
field: "permissions",
|
|
879
|
+
headerName: m.user_api_key_permissions(),
|
|
880
|
+
cellRenderer: ({ data }) => {
|
|
881
|
+
const permissions = formatPermissions(data.permissions);
|
|
913
882
|
if (permissions.length === 0) {
|
|
914
883
|
return (_jsx("span", { className: "text-muted-foreground text-sm", children: m.user_api_key_no_permissions() }));
|
|
915
884
|
}
|
|
@@ -931,10 +900,8 @@ const apiKeyRowActions = ({ m, onDelete, onEdit, }) => [
|
|
|
931
900
|
},
|
|
932
901
|
];
|
|
933
902
|
const getPermissionId = (resource, action) => `${resource}:${action}`;
|
|
934
|
-
const
|
|
935
|
-
|
|
936
|
-
Object.values(permissions).every((actions) => Array.isArray(actions) &&
|
|
937
|
-
actions.every((action) => typeof action === "string"));
|
|
903
|
+
const PermissionRecord = Schema.Record(Schema.String, Schema.Array(Schema.String));
|
|
904
|
+
const isPermissionRecord = Schema.is(PermissionRecord);
|
|
938
905
|
const formatPermissions = (permissions) => {
|
|
939
906
|
if (!isPermissionRecord(permissions))
|
|
940
907
|
return [];
|
|
@@ -957,17 +924,5 @@ const getSelectedPermissionObject = (options, selected) => {
|
|
|
957
924
|
}
|
|
958
925
|
return permissions;
|
|
959
926
|
};
|
|
960
|
-
const
|
|
961
|
-
|
|
962
|
-
user === null ||
|
|
963
|
-
!("twoFactorEnabled" in user))
|
|
964
|
-
return false;
|
|
965
|
-
return user.twoFactorEnabled === true;
|
|
966
|
-
};
|
|
967
|
-
const isAuthErrorResult = (result) => {
|
|
968
|
-
return (typeof result === "object" &&
|
|
969
|
-
result !== null &&
|
|
970
|
-
"error" in result &&
|
|
971
|
-
typeof result.error === "object" &&
|
|
972
|
-
result.error !== null);
|
|
973
|
-
};
|
|
927
|
+
const TwoFactorUser = Schema.Struct({ twoFactorEnabled: Schema.Boolean });
|
|
928
|
+
const hasTwoFactorEnabled = (user) => Option.exists(Schema.decodeUnknownOption(TwoFactorUser)(user), (decoded) => decoded.twoFactorEnabled);
|