@krak-stack/auth 0.1.18 → 0.1.20
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/organization-switcher.d.ts +2 -1
- package/dist/components/organization-switcher.js +17 -28
- package/dist/components/user-button.d.ts +2 -1
- package/dist/components/user-button.js +5 -5
- package/dist/components/utils.d.ts +3 -0
- package/dist/components/utils.js +22 -0
- package/package.json +1 -1
|
@@ -163,6 +163,7 @@ 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;
|
|
@@ -177,5 +178,5 @@ type OrganizationSummary = {
|
|
|
177
178
|
slug: string;
|
|
178
179
|
metadata?: unknown;
|
|
179
180
|
};
|
|
180
|
-
export declare function OrganizationSwitcher({ authClient, side, className, renderUnauthenticated, locked, messages, onChange, onCreate, }: OrganizationSwitcherProps): import("react").JSX.Element;
|
|
181
|
+
export declare function OrganizationSwitcher({ authClient, baseUrl, side, className, renderUnauthenticated, locked, messages, onChange, onCreate, }: OrganizationSwitcherProps): import("react").JSX.Element;
|
|
181
182
|
export {};
|
|
@@ -16,6 +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, assetUrl, isRecord } from "./utils";
|
|
19
20
|
const messages = {
|
|
20
21
|
en: {
|
|
21
22
|
api_key_rate_limit_notice: "API keys are subject to the same rate limits as your account.",
|
|
@@ -209,17 +210,6 @@ const isOrganizationSlugConflict = (error) => {
|
|
|
209
210
|
});
|
|
210
211
|
};
|
|
211
212
|
const nullableString = (value) => typeof value === "string" && value.trim() ? value.trim() : null;
|
|
212
|
-
const assetPath = (value) => {
|
|
213
|
-
if (!value?.trim())
|
|
214
|
-
return null;
|
|
215
|
-
try {
|
|
216
|
-
const url = new URL(value);
|
|
217
|
-
return url.pathname.startsWith("/api/assets/") ? url.pathname : value;
|
|
218
|
-
}
|
|
219
|
-
catch {
|
|
220
|
-
return value;
|
|
221
|
-
}
|
|
222
|
-
};
|
|
223
213
|
const parsePresignedUpload = (value, m) => {
|
|
224
214
|
if (!isRecord(value) ||
|
|
225
215
|
typeof value.uploadUrl !== "string" ||
|
|
@@ -228,7 +218,6 @@ const parsePresignedUpload = (value, m) => {
|
|
|
228
218
|
}
|
|
229
219
|
return { uploadUrl: value.uploadUrl, url: value.url };
|
|
230
220
|
};
|
|
231
|
-
const isRecord = (value) => typeof value === "object" && value !== null;
|
|
232
221
|
const uploadOrganizationLogo = async (authClient, file, m) => {
|
|
233
222
|
const contentType = file.type || "image/png";
|
|
234
223
|
const presignResult = await authClient.$fetch("../organizations/logo/presign", {
|
|
@@ -265,7 +254,7 @@ const findOrganizationTranslation = (organization, locale = currentOrganizationL
|
|
|
265
254
|
translations.find((translation) => translation.locale === "en") ??
|
|
266
255
|
translations[0]);
|
|
267
256
|
};
|
|
268
|
-
const organizationDisplay = (organization, m) => {
|
|
257
|
+
const organizationDisplay = (organization, m, baseUrl) => {
|
|
269
258
|
const translation = organization
|
|
270
259
|
? findOrganizationTranslation(organization)
|
|
271
260
|
: undefined;
|
|
@@ -274,10 +263,10 @@ const organizationDisplay = (organization, m) => {
|
|
|
274
263
|
organization?.name ||
|
|
275
264
|
m.organization_switcher_label(),
|
|
276
265
|
subtitle: organization?.slug ?? m.organization_switcher_label(),
|
|
277
|
-
image: translation?.icon || translation?.logo
|
|
266
|
+
image: assetUrl(translation?.icon || translation?.logo, baseUrl),
|
|
278
267
|
};
|
|
279
268
|
};
|
|
280
|
-
const organizationFormDefaults = (organization) => {
|
|
269
|
+
const organizationFormDefaults = (organization, baseUrl) => {
|
|
281
270
|
const translations = parseOrganizationMetadata(organization?.metadata).translations;
|
|
282
271
|
const en = translations.find((translation) => translation.locale === "en");
|
|
283
272
|
const fr = translations.find((translation) => translation.locale === "fr");
|
|
@@ -285,13 +274,13 @@ const organizationFormDefaults = (organization) => {
|
|
|
285
274
|
name: organization?.name ?? "",
|
|
286
275
|
slug: organization?.slug ?? "",
|
|
287
276
|
enName: en?.name || organization?.name || "",
|
|
288
|
-
enLogo: en?.logo
|
|
289
|
-
enIcon: en?.icon
|
|
277
|
+
enLogo: assetUrl(en?.logo, baseUrl) || null,
|
|
278
|
+
enIcon: assetUrl(en?.icon, baseUrl) || null,
|
|
290
279
|
enContactEmail: en?.contactEmail ?? "",
|
|
291
280
|
enLocation: en?.location ?? "",
|
|
292
281
|
frName: fr?.name ?? "",
|
|
293
|
-
frLogo: fr?.logo
|
|
294
|
-
frIcon: fr?.icon
|
|
282
|
+
frLogo: assetUrl(fr?.logo, baseUrl) || null,
|
|
283
|
+
frIcon: assetUrl(fr?.icon, baseUrl) || null,
|
|
295
284
|
frContactEmail: fr?.contactEmail ?? "",
|
|
296
285
|
frLocation: fr?.location ?? "",
|
|
297
286
|
};
|
|
@@ -353,7 +342,7 @@ const organizationMetadataFromForm = async (authClient, value, m) => {
|
|
|
353
342
|
}
|
|
354
343
|
return { translations };
|
|
355
344
|
};
|
|
356
|
-
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, onChange, onCreate, }) {
|
|
357
346
|
const labels = organizationSwitcherMessages(messages);
|
|
358
347
|
const m = organizationMessageFns(labels);
|
|
359
348
|
const session = authClient.useSession();
|
|
@@ -410,7 +399,7 @@ export function OrganizationSwitcher({ authClient, side = "bottom", className, r
|
|
|
410
399
|
}
|
|
411
400
|
}
|
|
412
401
|
};
|
|
413
|
-
const activeDisplay = organizationDisplay(active ?? null, m);
|
|
402
|
+
const activeDisplay = organizationDisplay(active ?? null, m, baseUrl);
|
|
414
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
|
|
415
404
|
? activeDisplay.name
|
|
416
405
|
: 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
|
|
@@ -418,7 +407,7 @@ export function OrganizationSwitcher({ authClient, side = "bottom", className, r
|
|
|
418
407
|
: {}) }), _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
|
|
419
408
|
? { imageSrc: activeDisplay.image }
|
|
420
409
|
: {}) }) }), 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) => {
|
|
421
|
-
const display = organizationDisplay(organization, m);
|
|
410
|
+
const display = organizationDisplay(organization, m, baseUrl);
|
|
422
411
|
return (_jsx(DropdownMenuItem, { onClick: async () => {
|
|
423
412
|
const result = await authClient.organization.setActive({
|
|
424
413
|
organizationId: organization.id,
|
|
@@ -435,7 +424,7 @@ export function OrganizationSwitcher({ authClient, side = "bottom", className, r
|
|
|
435
424
|
void loadUserInvitations();
|
|
436
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) => {
|
|
437
426
|
setDialog((current) => open ? "create" : current === "create" ? 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_create_title() }), _jsx(DialogDescription, { children: m.organization_create_description() })] }), _jsx(Separator, {}), _jsx(CreateOrganizationSection, { authClient: authClient, onCreated: async (organization) => {
|
|
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, baseUrl: baseUrl, onCreated: async (organization) => {
|
|
439
428
|
onCreate?.(organization);
|
|
440
429
|
setDialog(null);
|
|
441
430
|
const activeResult = await authClient.organization.setActive({
|
|
@@ -446,7 +435,7 @@ export function OrganizationSwitcher({ authClient, side = "bottom", className, r
|
|
|
446
435
|
onChange?.(organization);
|
|
447
436
|
} })] }) }), _jsx(Dialog, { open: dialog === "manage", onOpenChange: (open) => {
|
|
448
437
|
setDialog((current) => open ? "manage" : current === "manage" ? null : current);
|
|
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, organization: activeOrganization.data, onUpdated: refresh })) : null] }) }), _jsx(Dialog, { open: dialog === "members", onOpenChange: (open) => {
|
|
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, baseUrl: baseUrl, organization: activeOrganization.data, onUpdated: refresh })) : null] }) }), _jsx(Dialog, { open: dialog === "members", onOpenChange: (open) => {
|
|
450
439
|
setDialog((current) => open ? "members" : current === "members" ? null : current);
|
|
451
440
|
}, 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) => {
|
|
452
441
|
setDialog((current) => open ? "apiKeys" : current === "apiKeys" ? null : current);
|
|
@@ -528,10 +517,10 @@ const userInvitationColumns = ({ m, actingInvitationId, onAccept, onReject, }) =
|
|
|
528
517
|
},
|
|
529
518
|
]),
|
|
530
519
|
];
|
|
531
|
-
function EditOrganizationSection({ authClient, organization, onUpdated, }) {
|
|
520
|
+
function EditOrganizationSection({ authClient, baseUrl, organization, onUpdated, }) {
|
|
532
521
|
const m = useOrganizationMessages();
|
|
533
522
|
const [editingLocale, setEditingLocale] = useState(currentOrganizationLocale());
|
|
534
|
-
const defaultValues = organizationFormDefaults(organization);
|
|
523
|
+
const defaultValues = organizationFormDefaults(organization, baseUrl);
|
|
535
524
|
const form = useAppForm({
|
|
536
525
|
defaultValues,
|
|
537
526
|
onSubmit: async ({ value, formApi }) => {
|
|
@@ -603,10 +592,10 @@ function CreateOrganizationSlugAutoFill({ form, }) {
|
|
|
603
592
|
}, [form, name, slugIsDirty]);
|
|
604
593
|
return null;
|
|
605
594
|
}
|
|
606
|
-
function CreateOrganizationSection({ authClient, onCreated, }) {
|
|
595
|
+
function CreateOrganizationSection({ authClient, baseUrl, onCreated, }) {
|
|
607
596
|
const m = useOrganizationMessages();
|
|
608
597
|
const [editingLocale, setEditingLocale] = useState(currentOrganizationLocale());
|
|
609
|
-
const defaultValues = organizationFormDefaults();
|
|
598
|
+
const defaultValues = organizationFormDefaults(undefined, baseUrl);
|
|
610
599
|
const form = useAppForm({
|
|
611
600
|
defaultValues,
|
|
612
601
|
onSubmit: async ({ value, formApi }) => {
|
|
@@ -169,11 +169,12 @@ 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;
|
|
177
178
|
};
|
|
178
|
-
export declare const UserButton: ({ authClient, signOutRedirect, side, renderUnauthenticated, apiKeyPermissions, messages, }: UserDropdownProps) => import("react").JSX.Element;
|
|
179
|
+
export declare const UserButton: ({ authClient, baseUrl, signOutRedirect, side, renderUnauthenticated, apiKeyPermissions, messages, }: UserDropdownProps) => import("react").JSX.Element;
|
|
179
180
|
export {};
|
|
@@ -14,6 +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, assetUrl, isRecord } from "./utils";
|
|
17
18
|
const messages = {
|
|
18
19
|
en: {
|
|
19
20
|
api_key_rate_limit_notice: "API keys are subject to the same rate limits as your account.",
|
|
@@ -201,7 +202,6 @@ const userButtonMessageFns = (labels) => {
|
|
|
201
202
|
const UserButtonMessagesContext = createContext(userButtonMessageFns(userButtonMessages()));
|
|
202
203
|
const useUserButtonMessages = () => useContext(UserButtonMessagesContext);
|
|
203
204
|
const isFile = (value) => value instanceof File;
|
|
204
|
-
const isRecord = (value) => typeof value === "object" && value !== null;
|
|
205
205
|
const presignUserImageUpload = async (authClient, file, m) => {
|
|
206
206
|
const contentType = file.type || "image/png";
|
|
207
207
|
const result = await authClient.$fetch("/image/presign", {
|
|
@@ -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, }) => {
|
|
222
222
|
const labels = userButtonMessages(messages);
|
|
223
223
|
const m = userButtonMessageFns(labels);
|
|
224
224
|
const navigate = useNavigate();
|
|
@@ -233,7 +233,7 @@ export const UserButton = ({ authClient, signOutRedirect = "/", side = "bottom",
|
|
|
233
233
|
}
|
|
234
234
|
const displayName = session.user.name.trim();
|
|
235
235
|
const displayEmail = session.user.email.trim();
|
|
236
|
-
const displayImage = session.user.image
|
|
236
|
+
const displayImage = assetUrl(session.user.image, baseUrl);
|
|
237
237
|
const signOut = async () => {
|
|
238
238
|
const redirectUrl = signOutRedirect.startsWith("http")
|
|
239
239
|
? signOutRedirect
|
|
@@ -255,10 +255,10 @@ export const UserButton = ({ authClient, signOutRedirect = "/", side = "bottom",
|
|
|
255
255
|
if (!uploadResponse.ok) {
|
|
256
256
|
throw new Error(m.user_profile_image_upload_error());
|
|
257
257
|
}
|
|
258
|
-
return presigned.url;
|
|
258
|
+
return assetPath(presigned.url);
|
|
259
259
|
})()
|
|
260
260
|
: typeof values.image === "string"
|
|
261
|
-
? values.image
|
|
261
|
+
? assetPath(values.image)
|
|
262
262
|
: null;
|
|
263
263
|
const result = await authClient.updateUser({
|
|
264
264
|
name: values.name.trim(),
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export const isRecord = (value) => typeof value === "object" && value !== null;
|
|
2
|
+
export const assetPath = (value) => {
|
|
3
|
+
if (!value?.trim())
|
|
4
|
+
return null;
|
|
5
|
+
try {
|
|
6
|
+
const url = new URL(value);
|
|
7
|
+
return url.pathname.startsWith("/api/assets/") ? url.pathname : value;
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
return value;
|
|
11
|
+
}
|
|
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
|
+
};
|