@krak-stack/auth 0.1.16 → 0.1.17
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.
|
@@ -209,7 +209,6 @@ const isOrganizationSlugConflict = (error) => {
|
|
|
209
209
|
});
|
|
210
210
|
};
|
|
211
211
|
const nullableString = (value) => typeof value === "string" && value.trim() ? value.trim() : null;
|
|
212
|
-
const centralAuthUrl = (path) => new URL(path, import.meta.env.VITE_KRAKSTACK_AUTH_URL).toString();
|
|
213
212
|
const parsePresignedUpload = (value, m) => {
|
|
214
213
|
if (!isRecord(value) ||
|
|
215
214
|
typeof value.uploadUrl !== "string" ||
|
|
@@ -219,18 +218,16 @@ const parsePresignedUpload = (value, m) => {
|
|
|
219
218
|
return { uploadUrl: value.uploadUrl, url: value.url };
|
|
220
219
|
};
|
|
221
220
|
const isRecord = (value) => typeof value === "object" && value !== null;
|
|
222
|
-
const uploadOrganizationLogo = async (file, m) => {
|
|
221
|
+
const uploadOrganizationLogo = async (authClient, file, m) => {
|
|
223
222
|
const contentType = file.type || "image/png";
|
|
224
|
-
const
|
|
223
|
+
const presignResult = await authClient.$fetch("../organizations/logo/presign", {
|
|
225
224
|
method: "POST",
|
|
226
|
-
|
|
227
|
-
headers: { "Content-Type": "application/json" },
|
|
228
|
-
body: JSON.stringify({ fileName: file.name, contentType }),
|
|
225
|
+
body: { fileName: file.name, contentType },
|
|
229
226
|
});
|
|
230
|
-
if (
|
|
227
|
+
if (presignResult.error) {
|
|
231
228
|
throw new Error(m.organization_logo_upload_error());
|
|
232
229
|
}
|
|
233
|
-
const presigned = parsePresignedUpload(
|
|
230
|
+
const presigned = parsePresignedUpload(presignResult.data, m);
|
|
234
231
|
const uploadResponse = await fetch(presigned.uploadUrl, {
|
|
235
232
|
method: "PUT",
|
|
236
233
|
headers: { "Content-Type": contentType },
|
|
@@ -239,7 +236,7 @@ const uploadOrganizationLogo = async (file, m) => {
|
|
|
239
236
|
if (!uploadResponse.ok) {
|
|
240
237
|
throw new Error(m.organization_logo_upload_error());
|
|
241
238
|
}
|
|
242
|
-
return
|
|
239
|
+
return presigned.url;
|
|
243
240
|
};
|
|
244
241
|
const currentOrganizationLocale = () => getLocale() === "fr" ? "fr" : "en";
|
|
245
242
|
const parseOrganizationMetadata = (metadata) => {
|
|
@@ -291,7 +288,9 @@ const organizationFormDefaults = (organization) => {
|
|
|
291
288
|
frLocation: fr?.location ?? "",
|
|
292
289
|
};
|
|
293
290
|
};
|
|
294
|
-
const organizationLogoFromForm = async (file, fallback, m) =>
|
|
291
|
+
const organizationLogoFromForm = async (authClient, file, fallback, m) => file
|
|
292
|
+
? await uploadOrganizationLogo(authClient, file, m)
|
|
293
|
+
: nullableString(fallback);
|
|
295
294
|
const organizationRoles = ["owner", "admin", "member"];
|
|
296
295
|
const normalizeOrganizationRole = (role) => role === "owner" || role === "admin" || role === "member" ? role : "member";
|
|
297
296
|
const organizationRoleLabel = (role, m) => {
|
|
@@ -312,14 +311,14 @@ const initialsFromName = (name) => name
|
|
|
312
311
|
.map((part) => part[0])
|
|
313
312
|
.join("")
|
|
314
313
|
.toUpperCase() || "?";
|
|
315
|
-
const organizationMetadataFromForm = async (value, m) => {
|
|
314
|
+
const organizationMetadataFromForm = async (authClient, value, m) => {
|
|
316
315
|
const translations = [];
|
|
317
316
|
const enName = value.enName.trim() || value.name.trim();
|
|
318
317
|
const frName = value.frName.trim();
|
|
319
|
-
const enLogo = await organizationLogoFromForm(value.enLogo, value.enLogoUrl, m);
|
|
320
|
-
const enIcon = await organizationLogoFromForm(value.enIcon, value.enIconUrl, m);
|
|
321
|
-
const frLogo = await organizationLogoFromForm(value.frLogo, value.frLogoUrl, m);
|
|
322
|
-
const frIcon = await organizationLogoFromForm(value.frIcon, value.frIconUrl, m);
|
|
318
|
+
const enLogo = await organizationLogoFromForm(authClient, value.enLogo, value.enLogoUrl, m);
|
|
319
|
+
const enIcon = await organizationLogoFromForm(authClient, value.enIcon, value.enIconUrl, m);
|
|
320
|
+
const frLogo = await organizationLogoFromForm(authClient, value.frLogo, value.frLogoUrl, m);
|
|
321
|
+
const frIcon = await organizationLogoFromForm(authClient, value.frIcon, value.frIconUrl, m);
|
|
323
322
|
if (enName) {
|
|
324
323
|
translations.push({
|
|
325
324
|
locale: "en",
|
|
@@ -525,23 +524,35 @@ function EditOrganizationSection({ authClient, organization, onUpdated, }) {
|
|
|
525
524
|
defaultValues,
|
|
526
525
|
onSubmit: async ({ value, formApi }) => {
|
|
527
526
|
formApi.setErrorMap({ onSubmit: undefined });
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
527
|
+
try {
|
|
528
|
+
const name = value.name.trim();
|
|
529
|
+
const slug = value.slug.trim().toLowerCase();
|
|
530
|
+
const metadata = await organizationMetadataFromForm(authClient, value, m);
|
|
531
|
+
const result = await authClient.organization.update({
|
|
532
|
+
organizationId: organization.id,
|
|
533
|
+
data: { name, slug, metadata },
|
|
534
|
+
});
|
|
535
|
+
if (result.error) {
|
|
536
|
+
formApi.setErrorMap({
|
|
537
|
+
onSubmit: {
|
|
538
|
+
form: result.error.message ?? m.organization_update_error(),
|
|
539
|
+
fields: {},
|
|
540
|
+
},
|
|
541
|
+
});
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
await onUpdated();
|
|
545
|
+
}
|
|
546
|
+
catch (cause) {
|
|
536
547
|
formApi.setErrorMap({
|
|
537
548
|
onSubmit: {
|
|
538
|
-
form:
|
|
549
|
+
form: cause instanceof Error
|
|
550
|
+
? cause.message
|
|
551
|
+
: m.organization_update_error(),
|
|
539
552
|
fields: {},
|
|
540
553
|
},
|
|
541
554
|
});
|
|
542
|
-
return;
|
|
543
555
|
}
|
|
544
|
-
await onUpdated();
|
|
545
556
|
},
|
|
546
557
|
});
|
|
547
558
|
return (_jsx("section", { className: "flex flex-col gap-4", children: _jsx(form.AppForm, { children: _jsxs("form", { className: "flex max-w-xl flex-col gap-4", onSubmit: (event) => {
|
|
@@ -588,39 +599,51 @@ function CreateOrganizationSection({ authClient, onCreated, }) {
|
|
|
588
599
|
defaultValues,
|
|
589
600
|
onSubmit: async ({ value, formApi }) => {
|
|
590
601
|
formApi.setErrorMap({ onSubmit: undefined });
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
if (
|
|
602
|
+
try {
|
|
603
|
+
const name = value.name.trim();
|
|
604
|
+
const slug = (value.slug.trim() || slugify(name)).toLowerCase();
|
|
605
|
+
const metadata = await organizationMetadataFromForm(authClient, {
|
|
606
|
+
...value,
|
|
607
|
+
name,
|
|
608
|
+
slug,
|
|
609
|
+
}, m);
|
|
610
|
+
const result = await authClient.organization.create({
|
|
611
|
+
name,
|
|
612
|
+
slug,
|
|
613
|
+
metadata,
|
|
614
|
+
});
|
|
615
|
+
if (result.error) {
|
|
616
|
+
if (isOrganizationSlugConflict(result.error)) {
|
|
617
|
+
formApi.setErrorMap({
|
|
618
|
+
onSubmit: {
|
|
619
|
+
fields: {
|
|
620
|
+
slug: { message: m.organization_create_slug_conflict() },
|
|
621
|
+
},
|
|
622
|
+
},
|
|
623
|
+
});
|
|
624
|
+
return;
|
|
625
|
+
}
|
|
605
626
|
formApi.setErrorMap({
|
|
606
627
|
onSubmit: {
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
},
|
|
628
|
+
form: result.error.message ?? m.organization_create_error(),
|
|
629
|
+
fields: {},
|
|
610
630
|
},
|
|
611
631
|
});
|
|
612
632
|
return;
|
|
613
633
|
}
|
|
634
|
+
form.reset();
|
|
635
|
+
await onCreated(result.data);
|
|
636
|
+
}
|
|
637
|
+
catch (cause) {
|
|
614
638
|
formApi.setErrorMap({
|
|
615
639
|
onSubmit: {
|
|
616
|
-
form:
|
|
640
|
+
form: cause instanceof Error
|
|
641
|
+
? cause.message
|
|
642
|
+
: m.organization_create_error(),
|
|
617
643
|
fields: {},
|
|
618
644
|
},
|
|
619
645
|
});
|
|
620
|
-
return;
|
|
621
646
|
}
|
|
622
|
-
form.reset();
|
|
623
|
-
await onCreated(result.data);
|
|
624
647
|
},
|
|
625
648
|
});
|
|
626
649
|
return (_jsx("section", { className: "flex flex-col gap-4", children: _jsx(form.AppForm, { children: _jsxs("form", { className: "flex max-w-xl flex-col gap-4", onSubmit: (event) => {
|
|
@@ -201,25 +201,18 @@ const userButtonMessageFns = (labels) => {
|
|
|
201
201
|
const UserButtonMessagesContext = createContext(userButtonMessageFns(userButtonMessages()));
|
|
202
202
|
const useUserButtonMessages = () => useContext(UserButtonMessagesContext);
|
|
203
203
|
const isFile = (value) => value instanceof File;
|
|
204
|
-
const
|
|
205
|
-
|
|
206
|
-
"
|
|
207
|
-
const
|
|
208
|
-
const response = await fetch(authUrl("/api/auth/image/presign"), {
|
|
204
|
+
const isRecord = (value) => typeof value === "object" && value !== null;
|
|
205
|
+
const presignUserImageUpload = async (authClient, file, m) => {
|
|
206
|
+
const contentType = file.type || "image/png";
|
|
207
|
+
const result = await authClient.$fetch("/image/presign", {
|
|
209
208
|
method: "POST",
|
|
210
|
-
|
|
211
|
-
headers: { "Content-Type": "application/json" },
|
|
212
|
-
body: JSON.stringify({ fileName: file.name, contentType: file.type }),
|
|
209
|
+
body: { fileName: file.name, contentType },
|
|
213
210
|
});
|
|
214
|
-
if (!
|
|
211
|
+
if (result.error || !isRecord(result.data)) {
|
|
215
212
|
throw new Error(m.user_profile_image_upload_error());
|
|
216
213
|
}
|
|
217
|
-
const
|
|
218
|
-
|
|
219
|
-
throw new Error(m.user_profile_image_upload_error());
|
|
220
|
-
}
|
|
221
|
-
const uploadUrl = Reflect.get(body, "uploadUrl");
|
|
222
|
-
const url = Reflect.get(body, "url");
|
|
214
|
+
const uploadUrl = Reflect.get(result.data, "uploadUrl");
|
|
215
|
+
const url = Reflect.get(result.data, "url");
|
|
223
216
|
if (typeof uploadUrl !== "string" || typeof url !== "string") {
|
|
224
217
|
throw new Error(m.user_profile_image_upload_error());
|
|
225
218
|
}
|
|
@@ -240,9 +233,7 @@ export const UserButton = ({ authClient, signOutRedirect = "/", side = "bottom",
|
|
|
240
233
|
}
|
|
241
234
|
const displayName = session.user.name.trim();
|
|
242
235
|
const displayEmail = session.user.email.trim();
|
|
243
|
-
const displayImage = session.user.image?.trim()
|
|
244
|
-
? authUrl(session.user.image.trim())
|
|
245
|
-
: "";
|
|
236
|
+
const displayImage = session.user.image?.trim() ?? "";
|
|
246
237
|
const signOut = async () => {
|
|
247
238
|
const redirectUrl = signOutRedirect.startsWith("http")
|
|
248
239
|
? signOutRedirect
|
|
@@ -255,10 +246,10 @@ export const UserButton = ({ authClient, signOutRedirect = "/", side = "bottom",
|
|
|
255
246
|
const imageFile = isFile(values.image) ? values.image : null;
|
|
256
247
|
const image = imageFile
|
|
257
248
|
? await (async () => {
|
|
258
|
-
const presigned = await presignUserImageUpload(imageFile, m);
|
|
249
|
+
const presigned = await presignUserImageUpload(authClient, imageFile, m);
|
|
259
250
|
const uploadResponse = await fetch(presigned.uploadUrl, {
|
|
260
251
|
method: "PUT",
|
|
261
|
-
headers: { "Content-Type": imageFile.type },
|
|
252
|
+
headers: { "Content-Type": imageFile.type || "image/png" },
|
|
262
253
|
body: imageFile,
|
|
263
254
|
});
|
|
264
255
|
if (!uploadResponse.ok) {
|