@krak-stack/auth 0.1.19 → 0.1.21
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 +5 -1
- package/dist/components/organization-switcher.js +27 -18
- package/dist/components/user-button.d.ts +5 -1
- package/dist/components/user-button.js +13 -4
- package/dist/components/utils.d.ts +1 -0
- package/dist/components/utils.js +10 -0
- 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";
|
|
@@ -163,13 +163,16 @@ type OrganizationSwitcherMessageKey = keyof (typeof messages)["en"];
|
|
|
163
163
|
export type OrganizationSwitcherMessages = Partial<Record<OrganizationSwitcherMessageKey, string>>;
|
|
164
164
|
type OrganizationSwitcherProps = {
|
|
165
165
|
authClient: AuthUiClient;
|
|
166
|
+
baseUrl?: string | undefined;
|
|
166
167
|
side?: ComponentProps<typeof DropdownMenuContent>["side"];
|
|
167
168
|
className?: string;
|
|
168
169
|
renderUnauthenticated?: () => ReactNode;
|
|
169
170
|
locked?: boolean;
|
|
170
171
|
messages?: OrganizationSwitcherMessages;
|
|
172
|
+
dialog?: OrganizationSwitcherDialog | null;
|
|
171
173
|
onChange?: (organization: OrganizationSummary | null) => void;
|
|
172
174
|
onCreate?: (organization: OrganizationSummary) => void;
|
|
175
|
+
onDialogChange?: (dialog: OrganizationSwitcherDialog | null) => void;
|
|
173
176
|
};
|
|
174
177
|
type OrganizationSummary = {
|
|
175
178
|
id: string;
|
|
@@ -177,5 +180,6 @@ type OrganizationSummary = {
|
|
|
177
180
|
slug: string;
|
|
178
181
|
metadata?: unknown;
|
|
179
182
|
};
|
|
180
|
-
export
|
|
183
|
+
export type OrganizationSwitcherDialog = "create" | "manage" | "members" | "apiKeys" | "invitations";
|
|
184
|
+
export declare function OrganizationSwitcher({ authClient, baseUrl, side, className, renderUnauthenticated, locked, messages, dialog: controlledDialog, onChange, onCreate, onDialogChange, }: OrganizationSwitcherProps): import("react").JSX.Element;
|
|
181
185
|
export {};
|
|
@@ -16,7 +16,7 @@ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "
|
|
|
16
16
|
import { Separator } from "@/components/ui/separator";
|
|
17
17
|
import { cn } from "@/lib/utils";
|
|
18
18
|
import { OrganizationMetadata, } from "@krak-stack/auth/schema";
|
|
19
|
-
import { assetPath, isRecord } from "./utils";
|
|
19
|
+
import { assetPath, assetUrl, isRecord } from "./utils";
|
|
20
20
|
const messages = {
|
|
21
21
|
en: {
|
|
22
22
|
api_key_rate_limit_notice: "API keys are subject to the same rate limits as your account.",
|
|
@@ -254,7 +254,7 @@ const findOrganizationTranslation = (organization, locale = currentOrganizationL
|
|
|
254
254
|
translations.find((translation) => translation.locale === "en") ??
|
|
255
255
|
translations[0]);
|
|
256
256
|
};
|
|
257
|
-
const organizationDisplay = (organization, m) => {
|
|
257
|
+
const organizationDisplay = (organization, m, baseUrl) => {
|
|
258
258
|
const translation = organization
|
|
259
259
|
? findOrganizationTranslation(organization)
|
|
260
260
|
: undefined;
|
|
@@ -263,10 +263,10 @@ const organizationDisplay = (organization, m) => {
|
|
|
263
263
|
organization?.name ||
|
|
264
264
|
m.organization_switcher_label(),
|
|
265
265
|
subtitle: organization?.slug ?? m.organization_switcher_label(),
|
|
266
|
-
image: translation?.icon || translation?.logo
|
|
266
|
+
image: assetUrl(translation?.icon || translation?.logo, baseUrl),
|
|
267
267
|
};
|
|
268
268
|
};
|
|
269
|
-
const organizationFormDefaults = (organization) => {
|
|
269
|
+
const organizationFormDefaults = (organization, baseUrl) => {
|
|
270
270
|
const translations = parseOrganizationMetadata(organization?.metadata).translations;
|
|
271
271
|
const en = translations.find((translation) => translation.locale === "en");
|
|
272
272
|
const fr = translations.find((translation) => translation.locale === "fr");
|
|
@@ -274,13 +274,13 @@ const organizationFormDefaults = (organization) => {
|
|
|
274
274
|
name: organization?.name ?? "",
|
|
275
275
|
slug: organization?.slug ?? "",
|
|
276
276
|
enName: en?.name || organization?.name || "",
|
|
277
|
-
enLogo: en?.logo
|
|
278
|
-
enIcon: en?.icon
|
|
277
|
+
enLogo: assetUrl(en?.logo, baseUrl) || null,
|
|
278
|
+
enIcon: assetUrl(en?.icon, baseUrl) || null,
|
|
279
279
|
enContactEmail: en?.contactEmail ?? "",
|
|
280
280
|
enLocation: en?.location ?? "",
|
|
281
281
|
frName: fr?.name ?? "",
|
|
282
|
-
frLogo: fr?.logo
|
|
283
|
-
frIcon: fr?.icon
|
|
282
|
+
frLogo: assetUrl(fr?.logo, baseUrl) || null,
|
|
283
|
+
frIcon: assetUrl(fr?.icon, baseUrl) || null,
|
|
284
284
|
frContactEmail: fr?.contactEmail ?? "",
|
|
285
285
|
frLocation: fr?.location ?? "",
|
|
286
286
|
};
|
|
@@ -342,13 +342,14 @@ const organizationMetadataFromForm = async (authClient, value, m) => {
|
|
|
342
342
|
}
|
|
343
343
|
return { translations };
|
|
344
344
|
};
|
|
345
|
-
export function OrganizationSwitcher({ authClient, side = "bottom", className, renderUnauthenticated, locked = false, messages, onChange, onCreate, }) {
|
|
345
|
+
export function OrganizationSwitcher({ authClient, baseUrl, side = "bottom", className, 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(null);
|
|
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,14 @@ export function OrganizationSwitcher({ authClient, side = "bottom", className, r
|
|
|
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
|
+
};
|
|
370
379
|
useEffect(() => {
|
|
371
380
|
if (!sessionUserId)
|
|
372
381
|
return;
|
|
@@ -399,7 +408,7 @@ export function OrganizationSwitcher({ authClient, side = "bottom", className, r
|
|
|
399
408
|
}
|
|
400
409
|
}
|
|
401
410
|
};
|
|
402
|
-
const activeDisplay = organizationDisplay(active ?? null, m);
|
|
411
|
+
const activeDisplay = organizationDisplay(active ?? null, m, baseUrl);
|
|
403
412
|
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
|
|
404
413
|
? activeDisplay.name
|
|
405
414
|
: 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
|
|
@@ -407,7 +416,7 @@ export function OrganizationSwitcher({ authClient, side = "bottom", className, r
|
|
|
407
416
|
: {}) }), _jsx(ChevronsUpDown, { className: "text-muted-foreground size-4 shrink-0 group-data-[collapsible=icon]:hidden" })] }) }), _jsx(DropdownMenuContent, { className: "min-w-64 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: activeDisplay.name, subtitle: activeDisplay.subtitle, icon: Building2, variant: "sidebar", className: "px-1 py-1.5 text-left text-sm", ...(activeDisplay.image
|
|
408
417
|
? { imageSrc: activeDisplay.image }
|
|
409
418
|
: {}) }) }), hasOrganizationListItems ? _jsx(DropdownMenuSeparator, {}) : null, organizations.isPending ? (_jsx(DropdownMenuItem, { disabled: true, children: m.organization_loading() })) : null, organizations.error ? (_jsx(DropdownMenuItem, { disabled: true, children: organizations.error.message })) : null, !locked && selectableOrganizations.length ? (selectableOrganizations.map((organization) => {
|
|
410
|
-
const display = organizationDisplay(organization, m);
|
|
419
|
+
const display = organizationDisplay(organization, m, baseUrl);
|
|
411
420
|
return (_jsx(DropdownMenuItem, { onClick: async () => {
|
|
412
421
|
const result = await authClient.organization.setActive({
|
|
413
422
|
organizationId: organization.id,
|
|
@@ -424,7 +433,7 @@ export function OrganizationSwitcher({ authClient, side = "bottom", className, r
|
|
|
424
433
|
void loadUserInvitations();
|
|
425
434
|
}, 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
435
|
setDialog((current) => open ? "create" : current === "create" ? null : current);
|
|
427
|
-
}, 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, onCreated: async (organization) => {
|
|
436
|
+
}, 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
437
|
onCreate?.(organization);
|
|
429
438
|
setDialog(null);
|
|
430
439
|
const activeResult = await authClient.organization.setActive({
|
|
@@ -435,7 +444,7 @@ export function OrganizationSwitcher({ authClient, side = "bottom", className, r
|
|
|
435
444
|
onChange?.(organization);
|
|
436
445
|
} })] }) }), _jsx(Dialog, { open: dialog === "manage", onOpenChange: (open) => {
|
|
437
446
|
setDialog((current) => open ? "manage" : current === "manage" ? null : current);
|
|
438
|
-
}, 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, organization: activeOrganization.data, onUpdated: refresh })) : null] }) }), _jsx(Dialog, { open: dialog === "members", onOpenChange: (open) => {
|
|
447
|
+
}, 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) => {
|
|
439
448
|
setDialog((current) => open ? "members" : current === "members" ? null : current);
|
|
440
449
|
}, children: _jsxs(DialogContent, { className: "max-h-[85vh] overflow-y-auto sm:max-w-4xl", children: [_jsxs(DialogHeader, { children: [_jsx(DialogTitle, { className: "text-2xl", children: m.organization_members_title() }), _jsx(DialogDescription, { children: activeOrganization.data?.name })] }), _jsx(Separator, {}), activeOrganization.data ? (_jsx(OrganizationMembersManager, { authClient: authClient, organization: activeOrganization.data, currentUserId: session.data.user.id })) : null] }) }), _jsx(Dialog, { open: dialog === "apiKeys", onOpenChange: (open) => {
|
|
441
450
|
setDialog((current) => open ? "apiKeys" : current === "apiKeys" ? null : current);
|
|
@@ -517,10 +526,10 @@ const userInvitationColumns = ({ m, actingInvitationId, onAccept, onReject, }) =
|
|
|
517
526
|
},
|
|
518
527
|
]),
|
|
519
528
|
];
|
|
520
|
-
function EditOrganizationSection({ authClient, organization, onUpdated, }) {
|
|
529
|
+
function EditOrganizationSection({ authClient, baseUrl, organization, onUpdated, }) {
|
|
521
530
|
const m = useOrganizationMessages();
|
|
522
531
|
const [editingLocale, setEditingLocale] = useState(currentOrganizationLocale());
|
|
523
|
-
const defaultValues = organizationFormDefaults(organization);
|
|
532
|
+
const defaultValues = organizationFormDefaults(organization, baseUrl);
|
|
524
533
|
const form = useAppForm({
|
|
525
534
|
defaultValues,
|
|
526
535
|
onSubmit: async ({ value, formApi }) => {
|
|
@@ -592,10 +601,10 @@ function CreateOrganizationSlugAutoFill({ form, }) {
|
|
|
592
601
|
}, [form, name, slugIsDirty]);
|
|
593
602
|
return null;
|
|
594
603
|
}
|
|
595
|
-
function CreateOrganizationSection({ authClient, onCreated, }) {
|
|
604
|
+
function CreateOrganizationSection({ authClient, baseUrl, onCreated, }) {
|
|
596
605
|
const m = useOrganizationMessages();
|
|
597
606
|
const [editingLocale, setEditingLocale] = useState(currentOrganizationLocale());
|
|
598
|
-
const defaultValues = organizationFormDefaults();
|
|
607
|
+
const defaultValues = organizationFormDefaults(undefined, baseUrl);
|
|
599
608
|
const form = useAppForm({
|
|
600
609
|
defaultValues,
|
|
601
610
|
onSubmit: async ({ value, formApi }) => {
|
|
@@ -169,11 +169,15 @@ type UserButtonMessageKey = keyof (typeof messages)["en"];
|
|
|
169
169
|
export type UserButtonMessages = Partial<Record<UserButtonMessageKey, string>>;
|
|
170
170
|
type UserDropdownProps = {
|
|
171
171
|
authClient: AuthUiClient;
|
|
172
|
+
baseUrl?: string | undefined;
|
|
172
173
|
signOutRedirect?: string;
|
|
173
174
|
side?: ComponentProps<typeof DropdownMenuContent>["side"];
|
|
174
175
|
renderUnauthenticated?: () => ReactNode;
|
|
175
176
|
apiKeyPermissions?: Record<string, string[]>;
|
|
176
177
|
messages?: UserButtonMessages;
|
|
178
|
+
dialog?: UserButtonDialog | null;
|
|
179
|
+
onDialogChange?: (dialog: UserButtonDialog | null) => void;
|
|
177
180
|
};
|
|
178
|
-
export
|
|
181
|
+
export type UserButtonDialog = "account" | "security" | "apiKeys";
|
|
182
|
+
export declare const UserButton: ({ authClient, baseUrl, signOutRedirect, side, renderUnauthenticated, apiKeyPermissions, messages, dialog: controlledDialog, onDialogChange, }: UserDropdownProps) => import("react").JSX.Element;
|
|
179
183
|
export {};
|
|
@@ -14,7 +14,7 @@ import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, }
|
|
|
14
14
|
import { Field, FieldContent, FieldDescription, FieldGroup, FieldLabel, FieldLegend, FieldSet, } from "@/components/ui/field";
|
|
15
15
|
import { DropdownMenu, DropdownMenuContent, DropdownMenuGroup, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu";
|
|
16
16
|
import { Separator } from "@/components/ui/separator";
|
|
17
|
-
import { assetPath, isRecord } from "./utils";
|
|
17
|
+
import { assetPath, assetUrl, isRecord } from "./utils";
|
|
18
18
|
const messages = {
|
|
19
19
|
en: {
|
|
20
20
|
api_key_rate_limit_notice: "API keys are subject to the same rate limits as your account.",
|
|
@@ -218,7 +218,7 @@ const presignUserImageUpload = async (authClient, file, m) => {
|
|
|
218
218
|
}
|
|
219
219
|
return { uploadUrl, url };
|
|
220
220
|
};
|
|
221
|
-
export const UserButton = ({ authClient, signOutRedirect = "/", side = "bottom", renderUnauthenticated, apiKeyPermissions, messages, }) => {
|
|
221
|
+
export const UserButton = ({ authClient, baseUrl, signOutRedirect = "/", side = "bottom", renderUnauthenticated, apiKeyPermissions, messages, dialog: controlledDialog, onDialogChange, }) => {
|
|
222
222
|
const labels = userButtonMessages(messages);
|
|
223
223
|
const m = userButtonMessageFns(labels);
|
|
224
224
|
const navigate = useNavigate();
|
|
@@ -226,14 +226,23 @@ export const UserButton = ({ authClient, signOutRedirect = "/", side = "bottom",
|
|
|
226
226
|
select: (state) => `${import.meta.env.VITE_SITE_URL}${state.location.href}`,
|
|
227
227
|
});
|
|
228
228
|
const { data: session, isPending, refetch } = authClient.useSession();
|
|
229
|
-
const [
|
|
229
|
+
const [uncontrolledDialog, setUncontrolledDialog] = useState(null);
|
|
230
|
+
const settingsDialog = controlledDialog !== undefined ? controlledDialog : uncontrolledDialog;
|
|
230
231
|
const [formError, setFormError] = useState(null);
|
|
232
|
+
const setSettingsDialog = (next) => {
|
|
233
|
+
const nextDialog = typeof next === "function" ? next(settingsDialog) : next;
|
|
234
|
+
if (nextDialog === settingsDialog)
|
|
235
|
+
return;
|
|
236
|
+
if (controlledDialog === undefined)
|
|
237
|
+
setUncontrolledDialog(nextDialog);
|
|
238
|
+
onDialogChange?.(nextDialog);
|
|
239
|
+
};
|
|
231
240
|
if (!session) {
|
|
232
241
|
return _jsx(_Fragment, { children: renderUnauthenticated?.() });
|
|
233
242
|
}
|
|
234
243
|
const displayName = session.user.name.trim();
|
|
235
244
|
const displayEmail = session.user.email.trim();
|
|
236
|
-
const displayImage = session.user.image
|
|
245
|
+
const displayImage = assetUrl(session.user.image, baseUrl);
|
|
237
246
|
const signOut = async () => {
|
|
238
247
|
const redirectUrl = signOutRedirect.startsWith("http")
|
|
239
248
|
? signOutRedirect
|
package/dist/components/utils.js
CHANGED
|
@@ -10,3 +10,13 @@ export const assetPath = (value) => {
|
|
|
10
10
|
return value;
|
|
11
11
|
}
|
|
12
12
|
};
|
|
13
|
+
export const assetUrl = (value, baseUrl) => {
|
|
14
|
+
const path = value?.trim();
|
|
15
|
+
if (!path)
|
|
16
|
+
return "";
|
|
17
|
+
if (!path.startsWith("/api/assets/"))
|
|
18
|
+
return path;
|
|
19
|
+
if (!baseUrl?.trim())
|
|
20
|
+
return path;
|
|
21
|
+
return new URL(path, baseUrl).toString();
|
|
22
|
+
};
|
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";
|