@krak-stack/auth 0.1.20 → 0.1.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/index.d.ts +2 -0
- package/dist/components/organization-switcher.d.ts +6 -1
- package/dist/components/organization-switcher.js +18 -7
- package/dist/components/user-button.d.ts +6 -1
- package/dist/components/user-button.js +18 -12
- package/dist/index.d.ts +1 -1
- package/package.json +1 -1
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
export { OrganizationSwitcher } from "./organization-switcher";
|
|
2
2
|
export { UserButton } from "./user-button";
|
|
3
|
+
export type { OrganizationSwitcherDialog } from "./organization-switcher";
|
|
4
|
+
export type { UserButtonDialog } from "./user-button";
|
|
3
5
|
export type { AuthUiClient } from "./auth-client";
|
|
@@ -166,11 +166,15 @@ type OrganizationSwitcherProps = {
|
|
|
166
166
|
baseUrl?: string | undefined;
|
|
167
167
|
side?: ComponentProps<typeof DropdownMenuContent>["side"];
|
|
168
168
|
className?: string;
|
|
169
|
+
defaultDialog?: OrganizationSwitcherDialog | null;
|
|
170
|
+
hideTrigger?: boolean;
|
|
169
171
|
renderUnauthenticated?: () => ReactNode;
|
|
170
172
|
locked?: boolean;
|
|
171
173
|
messages?: OrganizationSwitcherMessages;
|
|
174
|
+
dialog?: OrganizationSwitcherDialog | null;
|
|
172
175
|
onChange?: (organization: OrganizationSummary | null) => void;
|
|
173
176
|
onCreate?: (organization: OrganizationSummary) => void;
|
|
177
|
+
onDialogChange?: (dialog: OrganizationSwitcherDialog | null) => void;
|
|
174
178
|
};
|
|
175
179
|
type OrganizationSummary = {
|
|
176
180
|
id: string;
|
|
@@ -178,5 +182,6 @@ type OrganizationSummary = {
|
|
|
178
182
|
slug: string;
|
|
179
183
|
metadata?: unknown;
|
|
180
184
|
};
|
|
181
|
-
export
|
|
185
|
+
export type OrganizationSwitcherDialog = "create" | "manage" | "members" | "apiKeys" | "invitations";
|
|
186
|
+
export declare function OrganizationSwitcher({ authClient, baseUrl, side, className, defaultDialog, hideTrigger, renderUnauthenticated, locked, messages, dialog: controlledDialog, onChange, onCreate, onDialogChange, }: OrganizationSwitcherProps): import("react").JSX.Element;
|
|
182
187
|
export {};
|
|
@@ -342,13 +342,14 @@ const organizationMetadataFromForm = async (authClient, value, m) => {
|
|
|
342
342
|
}
|
|
343
343
|
return { translations };
|
|
344
344
|
};
|
|
345
|
-
export function OrganizationSwitcher({ authClient, baseUrl, side = "bottom", className, renderUnauthenticated, locked = false, messages, onChange, onCreate, }) {
|
|
345
|
+
export function OrganizationSwitcher({ authClient, baseUrl, side = "bottom", className, defaultDialog = null, hideTrigger = false, renderUnauthenticated, locked = false, messages, dialog: controlledDialog, onChange, onCreate, onDialogChange, }) {
|
|
346
346
|
const labels = organizationSwitcherMessages(messages);
|
|
347
347
|
const m = organizationMessageFns(labels);
|
|
348
348
|
const session = authClient.useSession();
|
|
349
349
|
const organizations = authClient.useListOrganizations();
|
|
350
350
|
const activeOrganization = authClient.useActiveOrganization();
|
|
351
|
-
const [
|
|
351
|
+
const [uncontrolledDialog, setUncontrolledDialog] = useState(defaultDialog);
|
|
352
|
+
const dialog = controlledDialog !== undefined ? controlledDialog : uncontrolledDialog;
|
|
352
353
|
const sessionUserId = session.data?.user.id;
|
|
353
354
|
const [userInvitations, setUserInvitations] = useState([]);
|
|
354
355
|
const [userInvitationsError, setUserInvitationsError] = useState(null);
|
|
@@ -367,6 +368,15 @@ export function OrganizationSwitcher({ authClient, baseUrl, side = "bottom", cla
|
|
|
367
368
|
setUserInvitations(result.data ?? []);
|
|
368
369
|
setLoadingUserInvitations(false);
|
|
369
370
|
});
|
|
371
|
+
const setDialog = (next) => {
|
|
372
|
+
const nextDialog = typeof next === "function" ? next(dialog) : next;
|
|
373
|
+
if (nextDialog === dialog)
|
|
374
|
+
return;
|
|
375
|
+
if (controlledDialog === undefined)
|
|
376
|
+
setUncontrolledDialog(nextDialog);
|
|
377
|
+
onDialogChange?.(nextDialog);
|
|
378
|
+
};
|
|
379
|
+
const openCreate = () => setDialog("create");
|
|
370
380
|
useEffect(() => {
|
|
371
381
|
if (!sessionUserId)
|
|
372
382
|
return;
|
|
@@ -400,7 +410,7 @@ export function OrganizationSwitcher({ authClient, baseUrl, side = "bottom", cla
|
|
|
400
410
|
}
|
|
401
411
|
};
|
|
402
412
|
const activeDisplay = organizationDisplay(active ?? null, m, baseUrl);
|
|
403
|
-
return (_jsxs(OrganizationMessagesContext.Provider, { value: m, children: [_jsxs(DropdownMenu, { children: [_jsx(DropdownMenuTrigger, { render: _jsxs(Button, { variant: "ghost", className: cn("h-11 w-full justify-between gap-2 !px-1 !py-0 group-data-[collapsible=icon]:size-8 group-data-[collapsible=icon]:justify-start group-data-[collapsible=icon]:!p-0", className), children: [_jsx(AppBrand, { to: null, label: activeName
|
|
413
|
+
return (_jsxs(OrganizationMessagesContext.Provider, { value: m, children: [hideTrigger ? null : (_jsxs(DropdownMenu, { children: [_jsx(DropdownMenuTrigger, { render: _jsxs(Button, { variant: "ghost", className: cn("h-11 w-full justify-between gap-2 !px-1 !py-0 group-data-[collapsible=icon]:size-8 group-data-[collapsible=icon]:justify-start group-data-[collapsible=icon]:!p-0", className), children: [_jsx(AppBrand, { to: null, label: activeName
|
|
404
414
|
? activeDisplay.name
|
|
405
415
|
: m.organization_switcher_label(), subtitle: activeDisplay.subtitle, icon: Building2, variant: "sidebar", className: "min-w-0 flex-1 !p-0 text-left group-data-[collapsible=icon]:flex-none group-data-[collapsible=icon]:justify-start", ...(activeDisplay.image
|
|
406
416
|
? { imageSrc: activeDisplay.image }
|
|
@@ -419,20 +429,21 @@ export function OrganizationSwitcher({ authClient, baseUrl, side = "bottom", cla
|
|
|
419
429
|
}, children: _jsx(AppBrand, { to: null, label: display.name, subtitle: display.subtitle, icon: Building2, className: "w-full text-left [&>div:first-child]:size-7", ...(display.image ? { imageSrc: display.image } : {}) }) }, organization.id));
|
|
420
430
|
})) : !locked && !organizations.isPending ? (_jsx(DropdownMenuItem, { disabled: true, children: active
|
|
421
431
|
? m.organization_switcher_no_other_organizations()
|
|
422
|
-
: m.organization_switcher_empty() })) : null, _jsx(DropdownMenuSeparator, {}), !locked ? (_jsxs(DropdownMenuItem, { onClick:
|
|
432
|
+
: m.organization_switcher_empty() })) : null, _jsx(DropdownMenuSeparator, {}), !locked ? (_jsxs(DropdownMenuItem, { onClick: openCreate, children: [_jsx(Plus, {}), m.organization_create_title()] })) : null, _jsxs(DropdownMenuItem, { onClick: () => {
|
|
423
433
|
setDialog("invitations");
|
|
424
434
|
void loadUserInvitations();
|
|
425
|
-
}, children: [_jsx(Mail, {}), _jsxs("span", { className: "flex flex-1 items-center justify-between gap-3", children: [m.organization_user_invitations_title(), userInvitations.length ? (_jsx(Badge, { variant: "secondary", children: userInvitations.length })) : null] })] }), activeOrganization.data ? (_jsxs(_Fragment, { children: [_jsx(DropdownMenuSeparator, {}), _jsxs(DropdownMenuItem, { onClick: () => setDialog("manage"), children: [_jsx(PencilIcon, {}), m.organization_switcher_manage()] }), _jsxs(DropdownMenuItem, { onClick: () => setDialog("members"), children: [_jsx(Users, {}), m.organization_members_title()] }), _jsxs(DropdownMenuItem, { onClick: () => setDialog("apiKeys"), children: [_jsx(KeyRound, {}), m.user_button_api_keys()] })] })) : null] }) })] }), _jsx(Dialog, { open: dialog === "create", onOpenChange: (open) => {
|
|
435
|
+
}, children: [_jsx(Mail, {}), _jsxs("span", { className: "flex flex-1 items-center justify-between gap-3", children: [m.organization_user_invitations_title(), userInvitations.length ? (_jsx(Badge, { variant: "secondary", children: userInvitations.length })) : null] })] }), activeOrganization.data ? (_jsxs(_Fragment, { children: [_jsx(DropdownMenuSeparator, {}), _jsxs(DropdownMenuItem, { onClick: () => setDialog("manage"), children: [_jsx(PencilIcon, {}), m.organization_switcher_manage()] }), _jsxs(DropdownMenuItem, { onClick: () => setDialog("members"), children: [_jsx(Users, {}), m.organization_members_title()] }), _jsxs(DropdownMenuItem, { onClick: () => setDialog("apiKeys"), children: [_jsx(KeyRound, {}), m.user_button_api_keys()] })] })) : null] }) })] })), _jsx(Dialog, { open: dialog === "create", onOpenChange: (open) => {
|
|
426
436
|
setDialog((current) => open ? "create" : current === "create" ? null : current);
|
|
427
437
|
}, children: _jsxs(DialogContent, { className: "max-h-[85vh] overflow-y-auto sm:max-w-xl", children: [_jsxs(DialogHeader, { children: [_jsx(DialogTitle, { className: "text-2xl", children: m.organization_create_title() }), _jsx(DialogDescription, { children: m.organization_create_description() })] }), _jsx(Separator, {}), _jsx(CreateOrganizationSection, { authClient: authClient, baseUrl: baseUrl, onCreated: async (organization) => {
|
|
428
|
-
onCreate?.(organization);
|
|
429
438
|
setDialog(null);
|
|
430
439
|
const activeResult = await authClient.organization.setActive({
|
|
431
440
|
organizationId: organization.id,
|
|
432
441
|
});
|
|
433
442
|
await refresh();
|
|
434
|
-
if (!activeResult.error)
|
|
443
|
+
if (!activeResult.error) {
|
|
444
|
+
onCreate?.(organization);
|
|
435
445
|
onChange?.(organization);
|
|
446
|
+
}
|
|
436
447
|
} })] }) }), _jsx(Dialog, { open: dialog === "manage", onOpenChange: (open) => {
|
|
437
448
|
setDialog((current) => open ? "manage" : current === "manage" ? null : current);
|
|
438
449
|
}, children: _jsxs(DialogContent, { className: "max-h-[85vh] overflow-y-auto sm:max-w-xl", children: [_jsxs(DialogHeader, { children: [_jsx(DialogTitle, { className: "text-2xl", children: m.organization_switcher_manage() }), _jsx(DialogDescription, { children: m.organization_edit_description() })] }), _jsx(Separator, {}), activeOrganization.data ? (_jsx(EditOrganizationSection, { authClient: authClient, baseUrl: baseUrl, organization: activeOrganization.data, onUpdated: refresh })) : null] }) }), _jsx(Dialog, { open: dialog === "members", onOpenChange: (open) => {
|
|
@@ -172,9 +172,14 @@ type UserDropdownProps = {
|
|
|
172
172
|
baseUrl?: string | undefined;
|
|
173
173
|
signOutRedirect?: string;
|
|
174
174
|
side?: ComponentProps<typeof DropdownMenuContent>["side"];
|
|
175
|
+
defaultDialog?: UserButtonDialog | null;
|
|
176
|
+
hideTrigger?: boolean;
|
|
175
177
|
renderUnauthenticated?: () => ReactNode;
|
|
176
178
|
apiKeyPermissions?: Record<string, string[]>;
|
|
177
179
|
messages?: UserButtonMessages;
|
|
180
|
+
dialog?: UserButtonDialog | null;
|
|
181
|
+
onDialogChange?: (dialog: UserButtonDialog | null) => void;
|
|
178
182
|
};
|
|
179
|
-
export
|
|
183
|
+
export type UserButtonDialog = "account" | "security" | "apiKeys";
|
|
184
|
+
export declare const UserButton: ({ authClient, baseUrl, signOutRedirect, side, defaultDialog, hideTrigger, renderUnauthenticated, apiKeyPermissions, messages, dialog: controlledDialog, onDialogChange, }: UserDropdownProps) => import("react").JSX.Element;
|
|
180
185
|
export {};
|
|
@@ -186,19 +186,16 @@ const getLocale = () => (globalThis.document?.documentElement.lang ||
|
|
|
186
186
|
.startsWith("fr")
|
|
187
187
|
? "fr"
|
|
188
188
|
: "en";
|
|
189
|
-
const interpolate = (value, params) => params
|
|
190
|
-
? value.replace(/\{([^}]+)\}/g, (_, key) => String(params[key] ?? `{${key}}`))
|
|
191
|
-
: value;
|
|
192
189
|
const userButtonMessages = (overrides) => ({
|
|
193
190
|
...(getLocale().startsWith("fr") ? messages.fr : messages.en),
|
|
194
191
|
...overrides,
|
|
195
192
|
});
|
|
196
|
-
const
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
};
|
|
193
|
+
const interpolate = (value, params) => params
|
|
194
|
+
? value.replace(/\{([^}]+)\}/g, (_, key) => String(params[key] ?? `{${key}}`))
|
|
195
|
+
: value;
|
|
196
|
+
const userButtonMessageFns = (labels) => new Proxy({}, {
|
|
197
|
+
get: (_target, key) => (params) => interpolate(labels[key], params),
|
|
198
|
+
});
|
|
202
199
|
const UserButtonMessagesContext = createContext(userButtonMessageFns(userButtonMessages()));
|
|
203
200
|
const useUserButtonMessages = () => useContext(UserButtonMessagesContext);
|
|
204
201
|
const isFile = (value) => value instanceof File;
|
|
@@ -218,7 +215,7 @@ const presignUserImageUpload = async (authClient, file, m) => {
|
|
|
218
215
|
}
|
|
219
216
|
return { uploadUrl, url };
|
|
220
217
|
};
|
|
221
|
-
export const UserButton = ({ authClient, baseUrl, signOutRedirect = "/", side = "bottom", renderUnauthenticated, apiKeyPermissions, messages, }) => {
|
|
218
|
+
export const UserButton = ({ authClient, baseUrl, signOutRedirect = "/", side = "bottom", defaultDialog = null, hideTrigger = false, renderUnauthenticated, apiKeyPermissions, messages, dialog: controlledDialog, onDialogChange, }) => {
|
|
222
219
|
const labels = userButtonMessages(messages);
|
|
223
220
|
const m = userButtonMessageFns(labels);
|
|
224
221
|
const navigate = useNavigate();
|
|
@@ -226,8 +223,17 @@ export const UserButton = ({ authClient, baseUrl, signOutRedirect = "/", side =
|
|
|
226
223
|
select: (state) => `${import.meta.env.VITE_SITE_URL}${state.location.href}`,
|
|
227
224
|
});
|
|
228
225
|
const { data: session, isPending, refetch } = authClient.useSession();
|
|
229
|
-
const [
|
|
226
|
+
const [uncontrolledDialog, setUncontrolledDialog] = useState(defaultDialog);
|
|
227
|
+
const settingsDialog = controlledDialog !== undefined ? controlledDialog : uncontrolledDialog;
|
|
230
228
|
const [formError, setFormError] = useState(null);
|
|
229
|
+
const setSettingsDialog = (next) => {
|
|
230
|
+
const nextDialog = typeof next === "function" ? next(settingsDialog) : next;
|
|
231
|
+
if (nextDialog === settingsDialog)
|
|
232
|
+
return;
|
|
233
|
+
if (controlledDialog === undefined)
|
|
234
|
+
setUncontrolledDialog(nextDialog);
|
|
235
|
+
onDialogChange?.(nextDialog);
|
|
236
|
+
};
|
|
231
237
|
if (!session) {
|
|
232
238
|
return _jsx(_Fragment, { children: renderUnauthenticated?.() });
|
|
233
239
|
}
|
|
@@ -270,7 +276,7 @@ export const UserButton = ({ authClient, baseUrl, signOutRedirect = "/", side =
|
|
|
270
276
|
await refetch();
|
|
271
277
|
setSettingsDialog(null);
|
|
272
278
|
};
|
|
273
|
-
return (_jsxs(UserButtonMessagesContext.Provider, { value: m, children: [_jsxs(DropdownMenu, { children: [_jsx(DropdownMenuTrigger, { render: _jsxs(Button, { variant: "outline", size: "icon", children: [_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: [_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 } : {}) }) }), _jsx(DropdownMenuSeparator, {}), _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, {}), _jsxs(DropdownMenuItem, { onClick: signOut, children: [_jsx(LogOutIcon, {}), m.user_button_logout()] })] }) })] }), _jsx(Dialog, { open: settingsDialog === "account", onOpenChange: (open) => {
|
|
279
|
+
return (_jsxs(UserButtonMessagesContext.Provider, { value: m, children: [hideTrigger ? null : (_jsxs(DropdownMenu, { children: [_jsx(DropdownMenuTrigger, { render: _jsxs(Button, { variant: "outline", size: "icon", children: [_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: [_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 } : {}) }) }), _jsx(DropdownMenuSeparator, {}), _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, {}), _jsxs(DropdownMenuItem, { onClick: signOut, children: [_jsx(LogOutIcon, {}), m.user_button_logout()] })] }) })] })), _jsx(Dialog, { open: settingsDialog === "account", onOpenChange: (open) => {
|
|
274
280
|
setSettingsDialog((current) => open ? "account" : current === "account" ? null : current);
|
|
275
281
|
}, 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_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: {
|
|
276
282
|
name: displayName ?? "",
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
export * from "./schema";
|
|
2
2
|
export { AuthApiClients, AuthClient, type AuthClientLayerOptions, } from "./client";
|
|
3
3
|
export { AuthClientConfig } from "./config";
|
|
4
|
-
export { OrganizationSwitcher, UserButton, type AuthUiClient, } from "./components";
|
|
4
|
+
export { OrganizationSwitcher, UserButton, type AuthUiClient, type OrganizationSwitcherDialog, type UserButtonDialog, } from "./components";
|
|
5
5
|
export * as BetterAuth from "./better-auth";
|
|
6
6
|
export * as Extra from "./extra";
|
|
7
7
|
export * as Server from "./server";
|