@krak-stack/auth 0.1.44 → 0.1.45
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 +10 -2
- package/dist/admin/api.group.d.ts +13 -13
- package/dist/admin/schema.d.ts +13 -13
- package/dist/api.d.ts +104 -15
- package/dist/api.js +1 -1
- package/dist/better-auth/api.group.d.ts +33 -1
- package/dist/better-auth/api.group.js +15 -1
- package/dist/better-auth/schema.d.ts +36 -0
- package/dist/better-auth/schema.js +56 -0
- package/dist/components/admin-users.js +60 -4
- package/dist/components/auth-client-api.d.ts +135 -78
- package/dist/components/auth-client.js +3 -1
- package/dist/components/auth-provider.d.ts +17 -15
- package/dist/components/auth-provider.js +16 -2
- package/dist/components/index.d.ts +1 -1
- package/dist/components/organization-switcher.d.ts +5 -2
- package/dist/components/organization-switcher.js +64 -32
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -0
- package/dist/roles.d.ts +10 -0
- package/dist/roles.js +31 -0
- package/dist/schema.d.ts +1 -0
- package/dist/schema.js +2 -0
- package/dist/service.d.ts +500 -424
- package/package.json +5 -1
|
@@ -5,7 +5,7 @@ import {} from "@tanstack/react-table";
|
|
|
5
5
|
import { Effect, Schema } from "effect";
|
|
6
6
|
import { AsyncResult, Atom } from "effect/unstable/reactivity";
|
|
7
7
|
import { Building2, Check, ChevronsUpDown, Copy, KeyRound, Mail, PencilIcon, Plus, Trash2, UserIcon, UserPlus, Users, X, } from "lucide-react";
|
|
8
|
-
import { createContext, useContext, useEffect, useState, } from "react";
|
|
8
|
+
import { createContext, useContext, useEffect, useEffectEvent, useRef, useState, } from "react";
|
|
9
9
|
import { DataTable, DataTableRelationshipCell, } from "@/components/ui/data-table";
|
|
10
10
|
import { EditingLocaleSwitcher } from "@/components/ui/editing-locale-switcher";
|
|
11
11
|
import { AppBrand } from "@/components/ui/app-brand";
|
|
@@ -16,6 +16,7 @@ import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, }
|
|
|
16
16
|
import { DropdownMenu, DropdownMenuContent, DropdownMenuGroup, DropdownMenuItem, DropdownMenuLabel, DropdownMenuSeparator, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu";
|
|
17
17
|
import { Separator } from "@/components/ui/separator";
|
|
18
18
|
import { OrganizationMetadata, } from "@krak-stack/auth/schema";
|
|
19
|
+
import { hasAnyRole, isOrganizationRole, normalizeOrganizationRole, normalizeOrganizationRoles, organizationRoles, } from "../roles";
|
|
19
20
|
import { authClientApi } from "./auth-client-api";
|
|
20
21
|
import { useKrakstackAuth } from "./auth-provider";
|
|
21
22
|
import { createApiKey } from "./api-key";
|
|
@@ -74,6 +75,7 @@ const messages = {
|
|
|
74
75
|
organization_role_admin: "Admin",
|
|
75
76
|
organization_role_member: "Member",
|
|
76
77
|
organization_role_owner: "Owner",
|
|
78
|
+
organization_role_support: "Support",
|
|
77
79
|
organization_slug: "Organization slug",
|
|
78
80
|
organization_slug_description: "Leave blank to generate one from the name.",
|
|
79
81
|
organization_switcher_empty: "You do not belong to any organizations yet.",
|
|
@@ -153,6 +155,7 @@ const messages = {
|
|
|
153
155
|
organization_role_admin: "Admin",
|
|
154
156
|
organization_role_member: "Membre",
|
|
155
157
|
organization_role_owner: "Propriétaire",
|
|
158
|
+
organization_role_support: "Support",
|
|
156
159
|
organization_slug: "Slug de l'organisation",
|
|
157
160
|
organization_slug_description: "Laissez vide pour en générer un à partir du nom.",
|
|
158
161
|
organization_switcher_empty: "Vous n'appartenez encore à aucune organisation.",
|
|
@@ -335,14 +338,14 @@ const organizationLogoFromForm = async (value, m, uploadImage) => {
|
|
|
335
338
|
return assetPath(value);
|
|
336
339
|
return null;
|
|
337
340
|
};
|
|
338
|
-
const organizationRoles = ["owner", "admin", "member"];
|
|
339
|
-
const normalizeOrganizationRole = (role) => role === "owner" || role === "admin" || role === "member" ? role : "member";
|
|
340
341
|
const organizationRoleLabel = (role, m) => {
|
|
341
342
|
switch (normalizeOrganizationRole(role)) {
|
|
342
343
|
case "owner":
|
|
343
344
|
return m.organization_role_owner();
|
|
344
345
|
case "admin":
|
|
345
346
|
return m.organization_role_admin();
|
|
347
|
+
case "support":
|
|
348
|
+
return m.organization_role_support();
|
|
346
349
|
case "member":
|
|
347
350
|
return m.organization_role_member();
|
|
348
351
|
}
|
|
@@ -378,7 +381,7 @@ const organizationMetadataFromForm = async (value, m, uploadImage) => {
|
|
|
378
381
|
}
|
|
379
382
|
return { translations };
|
|
380
383
|
};
|
|
381
|
-
export function OrganizationSwitcher({ authClient: providedAuthClient, baseUrl, side = "bottom", className, defaultDialog = null, hideTrigger = false, renderUnauthenticated, locked = false, messages, dialog: controlledDialog, onChange, onCreate, onDialogChange, }) {
|
|
384
|
+
export function OrganizationSwitcher({ authClient: providedAuthClient, baseUrl, side = "bottom", className, defaultDialog = null, hideTrigger = false, menuActions, renderUnauthenticated, locked = false, messages, dialog: controlledDialog, onChange, onCreate, onDialogChange, }) {
|
|
382
385
|
const auth = useKrakstackAuth();
|
|
383
386
|
const authClient = providedAuthClient ?? auth?.authClient;
|
|
384
387
|
if (!authClient) {
|
|
@@ -389,6 +392,7 @@ export function OrganizationSwitcher({ authClient: providedAuthClient, baseUrl,
|
|
|
389
392
|
const session = authClient.useSession();
|
|
390
393
|
const organizations = authClient.useListOrganizations();
|
|
391
394
|
const activeOrganization = authClient.useActiveOrganization();
|
|
395
|
+
const lastAuthRefreshVersion = useRef(auth?.authRefreshVersion ?? 0);
|
|
392
396
|
const [uncontrolledDialog, setUncontrolledDialog] = useState(defaultDialog);
|
|
393
397
|
const dialog = controlledDialog !== undefined ? controlledDialog : uncontrolledDialog;
|
|
394
398
|
const invitationsAtom = session.data
|
|
@@ -418,6 +422,21 @@ export function OrganizationSwitcher({ authClient: providedAuthClient, baseUrl,
|
|
|
418
422
|
onDialogChange?.(nextDialog);
|
|
419
423
|
};
|
|
420
424
|
const openCreate = () => setDialog("create");
|
|
425
|
+
const refresh = async () => {
|
|
426
|
+
await organizations.refetch();
|
|
427
|
+
await activeOrganization.refetch();
|
|
428
|
+
await session.refetch();
|
|
429
|
+
};
|
|
430
|
+
const refreshOnSessionChanged = useEffectEvent(() => {
|
|
431
|
+
void refresh();
|
|
432
|
+
});
|
|
433
|
+
useEffect(() => {
|
|
434
|
+
const version = auth?.authRefreshVersion ?? 0;
|
|
435
|
+
if (version === lastAuthRefreshVersion.current)
|
|
436
|
+
return;
|
|
437
|
+
lastAuthRefreshVersion.current = version;
|
|
438
|
+
refreshOnSessionChanged();
|
|
439
|
+
}, [auth?.authRefreshVersion]);
|
|
421
440
|
if (!session.data) {
|
|
422
441
|
return _jsx(_Fragment, { children: renderUnauthenticated?.() });
|
|
423
442
|
}
|
|
@@ -427,11 +446,6 @@ export function OrganizationSwitcher({ authClient: providedAuthClient, baseUrl,
|
|
|
427
446
|
? (organizations.data?.filter((organization) => organization.id !== active?.id) ?? [])
|
|
428
447
|
: [];
|
|
429
448
|
const hasOrganizationListItems = organizations.isPending || Boolean(organizations.error) || !locked;
|
|
430
|
-
const refresh = async () => {
|
|
431
|
-
await organizations.refetch();
|
|
432
|
-
await activeOrganization.refetch();
|
|
433
|
-
await session.refetch();
|
|
434
|
-
};
|
|
435
449
|
const refreshAfterInvitationAction = async (previousActiveId) => {
|
|
436
450
|
refreshUserInvitations();
|
|
437
451
|
await refresh();
|
|
@@ -452,7 +466,7 @@ export function OrganizationSwitcher({ authClient: providedAuthClient, baseUrl,
|
|
|
452
466
|
? { imageSrc: activeDisplay.image }
|
|
453
467
|
: {}) }), _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
|
|
454
468
|
? { imageSrc: activeDisplay.image }
|
|
455
|
-
: {}) }) }), 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) => {
|
|
469
|
+
: {}) }) }), menuActions ? (_jsxs(_Fragment, { children: [_jsx(DropdownMenuSeparator, {}), menuActions] })) : null, 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) => {
|
|
456
470
|
const display = organizationDisplay(organization, m, baseUrl);
|
|
457
471
|
return (_jsx(DropdownMenuItem, { onClick: async () => {
|
|
458
472
|
const result = await authClient.organization.setActive({
|
|
@@ -750,14 +764,22 @@ function OrganizationMembersManager({ authClient, baseUrl, organization, current
|
|
|
750
764
|
});
|
|
751
765
|
const members = membersData.members;
|
|
752
766
|
const invitations = membersData.invitations;
|
|
767
|
+
const [memberRoleOverrides, setMemberRoleOverrides] = useState({});
|
|
768
|
+
const displayedMembers = members.map((member) => ({
|
|
769
|
+
...member,
|
|
770
|
+
role: memberRoleOverrides[member.id] ?? member.role,
|
|
771
|
+
}));
|
|
753
772
|
const membersError = AsyncResult.match(membersResult, {
|
|
754
773
|
onInitial: () => null,
|
|
755
774
|
onFailure: () => m.organization_members_load_error(),
|
|
756
775
|
onSuccess: () => null,
|
|
757
776
|
});
|
|
758
777
|
const loading = membersResult._tag === "Initial";
|
|
759
|
-
const currentMemberRole =
|
|
760
|
-
const canChangeMemberRoles = currentMemberRole
|
|
778
|
+
const currentMemberRole = displayedMembers.find((member) => member.userId === currentUserId)?.role;
|
|
779
|
+
const canChangeMemberRoles = hasAnyRole(currentMemberRole, [
|
|
780
|
+
"owner",
|
|
781
|
+
"admin",
|
|
782
|
+
]);
|
|
761
783
|
const canRemoveMembers = canChangeMemberRoles;
|
|
762
784
|
const inviteForm = useAppForm({
|
|
763
785
|
defaultValues: { email: "", role: "member" },
|
|
@@ -782,19 +804,26 @@ function OrganizationMembersManager({ authClient, baseUrl, organization, current
|
|
|
782
804
|
},
|
|
783
805
|
});
|
|
784
806
|
const updateRole = async (member, role) => {
|
|
785
|
-
setUpdatingMemberId(member.id);
|
|
786
807
|
setError(null);
|
|
808
|
+
const nextRole = Array.isArray(role)
|
|
809
|
+
? Array.from(new Set(role)).join(",")
|
|
810
|
+
: normalizeOrganizationRole(role);
|
|
811
|
+
setMemberRoleOverrides((current) => ({
|
|
812
|
+
...current,
|
|
813
|
+
[member.id]: nextRole,
|
|
814
|
+
}));
|
|
787
815
|
const result = await authClient.organization.updateMemberRole({
|
|
788
816
|
memberId: member.id,
|
|
789
|
-
role,
|
|
817
|
+
role: Array.isArray(role) ? role : normalizeOrganizationRole(role),
|
|
790
818
|
organizationId: organization.id,
|
|
791
819
|
});
|
|
792
|
-
setUpdatingMemberId(null);
|
|
793
820
|
if (result.error) {
|
|
821
|
+
setMemberRoleOverrides((current) => ({
|
|
822
|
+
...current,
|
|
823
|
+
[member.id]: member.role,
|
|
824
|
+
}));
|
|
794
825
|
setError(result.error.message ?? m.organization_member_role_error());
|
|
795
|
-
return;
|
|
796
826
|
}
|
|
797
|
-
refreshMembers();
|
|
798
827
|
};
|
|
799
828
|
const removeMember = async (member) => {
|
|
800
829
|
setUpdatingMemberId(member.id);
|
|
@@ -834,9 +863,8 @@ function OrganizationMembersManager({ authClient, baseUrl, organization, current
|
|
|
834
863
|
m,
|
|
835
864
|
baseUrl,
|
|
836
865
|
canChangeRoles: canChangeMemberRoles,
|
|
837
|
-
updatingMemberId,
|
|
838
866
|
onRoleChange: updateRole,
|
|
839
|
-
}), data:
|
|
867
|
+
}), data: displayedMembers, emptyLabel: loading ? m.user_loading() : m.organization_members_empty(), exportFileName: `${organization.slug}-members.csv`, features: { gallery: false }, searchState: "local", rowActions: memberRowActions({
|
|
840
868
|
m,
|
|
841
869
|
canRemoveMembers,
|
|
842
870
|
currentUserId,
|
|
@@ -849,7 +877,7 @@ function OrganizationMembersManager({ authClient, baseUrl, organization, current
|
|
|
849
877
|
onCancel: cancelInvitation,
|
|
850
878
|
}) }) })] })] }));
|
|
851
879
|
}
|
|
852
|
-
const memberColumns = ({ m, baseUrl, canChangeRoles,
|
|
880
|
+
const memberColumns = ({ m, baseUrl, canChangeRoles, onRoleChange, }) => [
|
|
853
881
|
{
|
|
854
882
|
id: "user",
|
|
855
883
|
header: m.organization_member_user(),
|
|
@@ -863,22 +891,26 @@ const memberColumns = ({ m, baseUrl, canChangeRoles, updatingMemberId, onRoleCha
|
|
|
863
891
|
header: m.organization_member_role(),
|
|
864
892
|
cell: ({ row }) => {
|
|
865
893
|
const member = row.original;
|
|
866
|
-
const
|
|
867
|
-
|
|
868
|
-
|
|
894
|
+
const roles = normalizeOrganizationRoles(member.role);
|
|
895
|
+
const roleOptions = roles.map((role) => ({
|
|
896
|
+
label: organizationRoleLabel(role, m),
|
|
897
|
+
value: role,
|
|
898
|
+
}));
|
|
899
|
+
if (!canChangeRoles) {
|
|
900
|
+
return (_jsx("div", { className: "flex flex-wrap gap-1", children: roleOptions.map((role) => (_jsx(Badge, { variant: "secondary", children: role.label }, role.value))) }));
|
|
869
901
|
}
|
|
870
902
|
return (_jsx(DataTableRelationshipCell, { emptyLabel: m.organization_member_role(), manageLabel: m.organization_member_role(), options: organizationRoles.map((role) => ({
|
|
871
903
|
label: organizationRoleLabel(role, m),
|
|
872
904
|
value: role,
|
|
873
|
-
})), value:
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
905
|
+
})), value: roleOptions, onAdd: (value) => {
|
|
906
|
+
if (!isOrganizationRole(value))
|
|
907
|
+
return;
|
|
908
|
+
onRoleChange(member, Array.from(new Set([...roles, value])));
|
|
909
|
+
}, onRemove: (value) => {
|
|
910
|
+
if (!isOrganizationRole(value))
|
|
911
|
+
return;
|
|
912
|
+
const nextRoles = roles.filter((role) => role !== value);
|
|
913
|
+
onRoleChange(member, nextRoles.length > 0 ? nextRoles : ["member"]);
|
|
882
914
|
} }));
|
|
883
915
|
},
|
|
884
916
|
},
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
export * from "./schema";
|
|
2
2
|
export * as Query from "./query";
|
|
3
|
+
export * as Roles from "./roles";
|
|
3
4
|
export { AuthClientApi, AuthServiceApi } from "./api";
|
|
4
5
|
export { AuthService, type AuthServiceLayerOptions } from "./service";
|
|
5
6
|
export { AuthClientConfig } from "./config";
|
|
6
7
|
export { proxyAuthRequest } from "./server/proxy";
|
|
7
|
-
export { OrganizationSwitcher, KrakstackAuthProvider, ResetPassword, Signin, Signup, TwoFactor, AdminOrganizationForm, AdminOrganizationsTable, AdminUsersTable, UserButton, assetUrl, createAuthUiClient, type AuthUiClient, type KrakstackAuthContextValue, type KrakstackAuthLocale, type KrakstackAuthProviderProps, type OrganizationSwitcherDialog, type UserButtonDialog, useAuthClient, useAdminUsersTotal, } from "./components";
|
|
8
|
+
export { OrganizationSwitcher, KrakstackAuthProvider, ResetPassword, Signin, Signup, TwoFactor, AdminOrganizationForm, AdminOrganizationsTable, AdminUsersTable, UserButton, assetUrl, createAuthUiClient, type AuthUiClient, type KrakstackAuthContextValue, type KrakstackAuthLocale, type KrakstackAuthProviderProps, type OrganizationSwitcherDialog, type OrganizationSwitcherProps, type UserButtonDialog, useAuthClient, useAdminUsersTotal, } from "./components";
|
|
8
9
|
export * as BetterAuth from "./better-auth/api.group";
|
|
9
10
|
export * as Extra from "./extra/api.group";
|
|
10
11
|
export * as Server from "./server";
|
package/dist/index.js
CHANGED
package/dist/roles.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export declare const organizationRoles: readonly ["owner", "admin", "support", "member"];
|
|
2
|
+
export type OrganizationRole = (typeof organizationRoles)[number];
|
|
3
|
+
export declare const organizationImpersonationRoles: readonly ["owner", "admin", "support"];
|
|
4
|
+
export declare const globalAdminRoles: readonly ["admin"];
|
|
5
|
+
export type GlobalAdminRole = (typeof globalAdminRoles)[number];
|
|
6
|
+
export declare const parseRoleList: (role: unknown) => string[];
|
|
7
|
+
export declare const hasAnyRole: (role: unknown, roles: readonly string[]) => boolean;
|
|
8
|
+
export declare const isOrganizationRole: (role: string) => role is OrganizationRole;
|
|
9
|
+
export declare const normalizeOrganizationRole: (role: unknown) => OrganizationRole;
|
|
10
|
+
export declare const normalizeOrganizationRoles: (role: unknown) => OrganizationRole[];
|
package/dist/roles.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export const organizationRoles = [
|
|
2
|
+
"owner",
|
|
3
|
+
"admin",
|
|
4
|
+
"support",
|
|
5
|
+
"member",
|
|
6
|
+
];
|
|
7
|
+
export const organizationImpersonationRoles = [
|
|
8
|
+
"owner",
|
|
9
|
+
"admin",
|
|
10
|
+
"support",
|
|
11
|
+
];
|
|
12
|
+
export const globalAdminRoles = ["admin"];
|
|
13
|
+
export const parseRoleList = (role) => typeof role === "string"
|
|
14
|
+
? role
|
|
15
|
+
.split(",")
|
|
16
|
+
.map((item) => item.trim())
|
|
17
|
+
.filter(Boolean)
|
|
18
|
+
: [];
|
|
19
|
+
export const hasAnyRole = (role, roles) => {
|
|
20
|
+
const allowed = new Set(roles);
|
|
21
|
+
return parseRoleList(role).some((item) => allowed.has(item));
|
|
22
|
+
};
|
|
23
|
+
export const isOrganizationRole = (role) => organizationRoles.includes(role);
|
|
24
|
+
export const normalizeOrganizationRole = (role) => {
|
|
25
|
+
const [firstRole] = parseRoleList(role).filter(isOrganizationRole);
|
|
26
|
+
return firstRole ?? "member";
|
|
27
|
+
};
|
|
28
|
+
export const normalizeOrganizationRoles = (role) => {
|
|
29
|
+
const roles = parseRoleList(role).filter(isOrganizationRole);
|
|
30
|
+
return roles.length > 0 ? Array.from(new Set(roles)) : ["member"];
|
|
31
|
+
};
|
package/dist/schema.d.ts
CHANGED
|
@@ -21,6 +21,7 @@ export declare const Session: Schema.Struct<{
|
|
|
21
21
|
readonly userAgent: Schema.optional<Schema.NullOr<Schema.String>>;
|
|
22
22
|
readonly userId: Schema.String;
|
|
23
23
|
readonly impersonatedBy: Schema.optional<Schema.NullOr<Schema.String>>;
|
|
24
|
+
readonly impersonatedByOrganizationId: Schema.optional<Schema.NullOr<Schema.String>>;
|
|
24
25
|
readonly activeOrganizationId: Schema.optional<Schema.NullOr<Schema.String>>;
|
|
25
26
|
}>;
|
|
26
27
|
export declare const OrganizationLocale: Schema.Union<readonly [Schema.Literal<"en">, Schema.Literal<"fr">]>;
|
package/dist/schema.js
CHANGED
|
@@ -39,6 +39,7 @@ export const Session = Schema.Struct({
|
|
|
39
39
|
userAgent: Schema.optional(Schema.NullOr(Schema.String)),
|
|
40
40
|
userId: Schema.String,
|
|
41
41
|
impersonatedBy: Schema.optional(Schema.NullOr(Schema.String)),
|
|
42
|
+
impersonatedByOrganizationId: Schema.optional(Schema.NullOr(Schema.String)),
|
|
42
43
|
activeOrganizationId: Schema.optional(Schema.NullOr(Schema.String)),
|
|
43
44
|
}).annotate({
|
|
44
45
|
identifier: "Session",
|
|
@@ -55,6 +56,7 @@ export const Session = Schema.Struct({
|
|
|
55
56
|
userAgent: "Mozilla/5.0",
|
|
56
57
|
userId: "user_1",
|
|
57
58
|
impersonatedBy: null,
|
|
59
|
+
impersonatedByOrganizationId: null,
|
|
58
60
|
activeOrganizationId: "org_1",
|
|
59
61
|
},
|
|
60
62
|
],
|