@krak-stack/auth 0.1.40 → 0.1.41

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.
@@ -9,11 +9,9 @@ import { DataTable } from "@/components/ui/data-table";
9
9
  import { ErrorMessage } from "@/components/ui/form";
10
10
  import { AppBrand } from "@/components/ui/app-brand";
11
11
  import { Badge } from "@/components/ui/badge";
12
- import { assetUrl } from "@/lib/assets";
13
- import { authBaseUrl } from "@/services/auth/client";
14
- import { organizationBranding } from "@/services/organizations/branding";
15
- import { AdminApiClient } from "@/lib/admin-api-client";
12
+ import { authClientApi } from "./auth-client-api";
16
13
  import { useKrakstackAuth } from "./auth-provider";
14
+ import { assetUrl, organizationBranding } from "./utils";
17
15
  const defaultMessages = {
18
16
  en: {
19
17
  admin_column_created: "Created",
@@ -35,11 +33,11 @@ const defaultMessages = {
35
33
  const labels = (locale) => ({
36
34
  ...defaultMessages[locale],
37
35
  });
38
- const organizationDisplay = (organization, locale) => {
36
+ const organizationDisplay = (organization, locale, baseUrl) => {
39
37
  const branding = organizationBranding(organization, locale);
40
38
  return {
41
39
  name: branding?.name ?? organization.name,
42
- image: assetUrl(branding?.logo, authBaseUrl),
40
+ image: assetUrl(branding?.logo, baseUrl),
43
41
  };
44
42
  };
45
43
  const organizationsQuery = (search, projectId) => {
@@ -55,9 +53,9 @@ const organizationsQuery = (search, projectId) => {
55
53
  key: JSON.stringify(query),
56
54
  };
57
55
  };
58
- const organizationsAtom = Atom.family(({ projectId, reloadKey, search, }) => {
56
+ const organizationsAtom = Atom.family(({ baseUrl, projectId, reloadKey, search, }) => {
59
57
  const request = organizationsQuery(search, projectId);
60
- return AdminApiClient.query("admin", "listOrganizations", {
58
+ return authClientApi(baseUrl).query("admin", "listOrganizations", {
61
59
  query: request.query,
62
60
  timeToLive: "1 minute",
63
61
  reactivityKeys: [
@@ -71,9 +69,15 @@ export function AdminOrganizationsTable({ from, reloadKey = 0, search, }) {
71
69
  const auth = useKrakstackAuth();
72
70
  const locale = auth?.locale ?? "en";
73
71
  const m = labels(locale);
72
+ const baseUrl = auth?.baseUrl;
74
73
  const projectId = auth?.projectId;
75
74
  const [refreshKey, setRefreshKey] = useState(0);
76
- const result = useAtomValue(organizationsAtom({ projectId, reloadKey: reloadKey + refreshKey, search }));
75
+ const result = useAtomValue(organizationsAtom({
76
+ baseUrl,
77
+ projectId,
78
+ reloadKey: reloadKey + refreshKey,
79
+ search,
80
+ }));
77
81
  const organizations = AsyncResult.match(result, {
78
82
  onInitial: () => [],
79
83
  onFailure: () => [],
@@ -94,14 +98,14 @@ export function AdminOrganizationsTable({ from, reloadKey = 0, search, }) {
94
98
  onFailure: () => false,
95
99
  onSuccess: () => false,
96
100
  });
97
- return (_jsxs("div", { className: "flex flex-col gap-4", children: [error ? _jsx(ErrorMessage, { text: error }) : null, _jsx(DataTable, { columns: organizationColumns(m, locale), data: organizations, exportFileName: "organizations.csv", features: { gallery: false }, ...(from ? { from } : {}), isLoading: isLoading, onRefresh: () => setRefreshKey((current) => current + 1), serverPagination: { rowCount: total } })] }));
101
+ return (_jsxs("div", { className: "flex flex-col gap-4", children: [error ? _jsx(ErrorMessage, { text: error }) : null, _jsx(DataTable, { columns: organizationColumns(m, locale, baseUrl), data: organizations, exportFileName: "organizations.csv", features: { gallery: false }, ...(from ? { from } : {}), isLoading: isLoading, onRefresh: () => setRefreshKey((current) => current + 1), serverPagination: { rowCount: total } })] }));
98
102
  }
99
- const organizationColumns = (m, locale) => [
103
+ const organizationColumns = (m, locale, baseUrl) => [
100
104
  {
101
105
  accessorKey: "name",
102
106
  header: m.admin_column_organization,
103
107
  cell: ({ row }) => {
104
- const display = organizationDisplay(row.original, locale);
108
+ const display = organizationDisplay(row.original, locale, baseUrl);
105
109
  return (_jsx(AppBrand, { to: null, label: display.name, subtitle: row.original.slug, icon: Building2, className: "min-w-56", ...(display.image ? { imageSrc: display.image } : {}) }));
106
110
  },
107
111
  },
@@ -5,4 +5,4 @@ export declare function AdminUsersTable({ from, reloadKey, search, }: {
5
5
  reloadKey?: number;
6
6
  search: TableParams;
7
7
  }): import("react").JSX.Element;
8
- export declare function useAdminUsersTotal(search: TableParams): unknown;
8
+ export declare function useAdminUsersTotal(search: TableParams): number;
@@ -11,10 +11,9 @@ import { ErrorMessage } from "@/components/ui/form";
11
11
  import { AppBrand } from "@/components/ui/app-brand";
12
12
  import { Badge } from "@/components/ui/badge";
13
13
  import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, } from "@/components/ui/alert-dialog";
14
- import { authBaseUrl, authClient } from "@/services/auth/client";
15
- import { assetUrl } from "@/lib/assets";
16
- import { AdminApiClient } from "@/lib/admin-api-client";
14
+ import { authClientApi } from "./auth-client-api";
17
15
  import { useKrakstackAuth } from "./auth-provider";
16
+ import { assetUrl } from "./utils";
18
17
  const defaultMessages = {
19
18
  en: {
20
19
  admin_action_ban: "Ban",
@@ -80,9 +79,9 @@ const adminUsersQuery = (search, projectId) => {
80
79
  key: JSON.stringify(query),
81
80
  };
82
81
  };
83
- const usersAtom = Atom.family(({ projectId, reloadKey, search, }) => {
82
+ const usersAtom = Atom.family(({ baseUrl, projectId, reloadKey, search, }) => {
84
83
  const request = adminUsersQuery(search, projectId);
85
- return AdminApiClient.query("admin", "listUsers", {
84
+ return authClientApi(baseUrl).query("admin", "listUsers", {
86
85
  query: request.query,
87
86
  timeToLive: "1 minute",
88
87
  reactivityKeys: [
@@ -92,17 +91,17 @@ const usersAtom = Atom.family(({ projectId, reloadKey, search, }) => {
92
91
  serializationKey: `admin-users:${reloadKey}:${request.key}`,
93
92
  });
94
93
  });
95
- const banUser = async (userId, m) => {
94
+ const banUser = async (authClient, userId, m) => {
96
95
  const result = await authClient.admin.banUser({ userId });
97
96
  if (result.error)
98
97
  throw new Error(result.error.message ?? m.admin_error_ban);
99
98
  };
100
- const unbanUser = async (userId, m) => {
99
+ const unbanUser = async (authClient, userId, m) => {
101
100
  const result = await authClient.admin.unbanUser({ userId });
102
101
  if (result.error)
103
102
  throw new Error(result.error.message ?? m.admin_error_unban);
104
103
  };
105
- const impersonateUser = async (userId, m) => {
104
+ const impersonateUser = async (authClient, userId, m) => {
106
105
  const result = await authClient.admin.impersonateUser({ userId });
107
106
  if (result.error)
108
107
  throw new Error(result.error.message ?? m.admin_error_impersonate);
@@ -111,11 +110,18 @@ export function AdminUsersTable({ from, reloadKey = 0, search, }) {
111
110
  const navigate = useNavigate();
112
111
  const auth = useKrakstackAuth();
113
112
  const m = labels(auth?.locale ?? "en");
113
+ const authClient = auth?.authClient;
114
+ const baseUrl = auth?.baseUrl;
114
115
  const projectId = auth?.projectId;
115
116
  const [impersonatingUserId, setImpersonatingUserId] = useState(null);
116
117
  const [impersonateError, setImpersonateError] = useState("");
117
118
  const [refreshKey, setRefreshKey] = useState(0);
118
- const result = useAtomValue(usersAtom({ projectId, search, reloadKey: reloadKey + refreshKey }));
119
+ const result = useAtomValue(usersAtom({
120
+ baseUrl,
121
+ projectId,
122
+ search,
123
+ reloadKey: reloadKey + refreshKey,
124
+ }));
119
125
  const [banningUser, setBanningUser] = useState(null);
120
126
  const [unbanningUser, setUnbanningUser] = useState(null);
121
127
  const users = AsyncResult.match(result, {
@@ -146,7 +152,9 @@ export function AdminUsersTable({ from, reloadKey = 0, search, }) {
146
152
  setImpersonateError("");
147
153
  setImpersonatingUserId(user.id);
148
154
  try {
149
- await impersonateUser(user.id, m);
155
+ if (!authClient)
156
+ throw new Error(m.admin_error_access_required);
157
+ await impersonateUser(authClient, user.id, m);
150
158
  await navigate({ to: "/" });
151
159
  }
152
160
  catch (cause) {
@@ -173,11 +181,16 @@ export function AdminUsersTable({ from, reloadKey = 0, search, }) {
173
181
  onClick: (user) => setUnbanningUser(user),
174
182
  visible: (user) => !!user.banned,
175
183
  },
176
- ] }), banningUser ? (_jsx(BanUserDialog, { labels: m, user: banningUser, onBanned: () => setRefreshKey((current) => current + 1), onClose: () => setBanningUser(null) })) : null, unbanningUser ? (_jsx(UnbanUserDialog, { labels: m, user: unbanningUser, onUnbanned: () => setRefreshKey((current) => current + 1), onClose: () => setUnbanningUser(null) })) : null] }));
184
+ ] }), banningUser ? (_jsx(BanUserDialog, { labels: m, authClient: authClient, user: banningUser, onBanned: () => setRefreshKey((current) => current + 1), onClose: () => setBanningUser(null) })) : null, unbanningUser ? (_jsx(UnbanUserDialog, { labels: m, authClient: authClient, user: unbanningUser, onUnbanned: () => setRefreshKey((current) => current + 1), onClose: () => setUnbanningUser(null) })) : null] }));
177
185
  }
178
186
  export function useAdminUsersTotal(search) {
179
- const projectId = useKrakstackAuth()?.projectId;
180
- const result = useAtomValue(usersAtom({ projectId, search, reloadKey: 0 }));
187
+ const auth = useKrakstackAuth();
188
+ const result = useAtomValue(usersAtom({
189
+ baseUrl: auth?.baseUrl,
190
+ projectId: auth?.projectId,
191
+ search,
192
+ reloadKey: 0,
193
+ }));
181
194
  return AsyncResult.match(result, {
182
195
  onInitial: () => 0,
183
196
  onFailure: () => 0,
@@ -189,7 +202,7 @@ const userColumns = (m) => [
189
202
  accessorKey: "email",
190
203
  header: m.admin_column_user,
191
204
  cell: ({ row }) => {
192
- const image = assetUrl(row.original.image, authBaseUrl);
205
+ const image = assetUrl(row.original.image);
193
206
  return (_jsx(AppBrand, { to: null, label: row.original.email, subtitle: row.original.name, icon: UserIcon, className: "min-w-48", ...(image ? { imageSrc: image } : {}) }));
194
207
  },
195
208
  },
@@ -219,7 +232,7 @@ const userColumns = (m) => [
219
232
  cell: ({ row }) => (_jsx("span", { className: "text-muted-foreground text-sm", children: new Date(row.original.createdAt).toLocaleDateString() })),
220
233
  },
221
234
  ];
222
- function BanUserDialog({ labels: m, user, onBanned, onClose, }) {
235
+ function BanUserDialog({ labels: m, authClient, user, onBanned, onClose, }) {
223
236
  const [isPending, setIsPending] = useState(false);
224
237
  const [error, setError] = useState("");
225
238
  return (_jsx(AlertDialog, { open: true, onOpenChange: (open) => !open && onClose(), children: _jsxs(AlertDialogContent, { children: [_jsxs(AlertDialogHeader, { children: [_jsx(AlertDialogTitle, { children: m.admin_ban_title }), _jsx(AlertDialogDescription, { children: text(m.admin_ban_description, {
@@ -229,7 +242,9 @@ function BanUserDialog({ labels: m, user, onBanned, onClose, }) {
229
242
  setError("");
230
243
  setIsPending(true);
231
244
  try {
232
- await banUser(user.id, m);
245
+ if (!authClient)
246
+ throw new Error(m.admin_error_access_required);
247
+ await banUser(authClient, user.id, m);
233
248
  onBanned();
234
249
  onClose();
235
250
  }
@@ -241,7 +256,7 @@ function BanUserDialog({ labels: m, user, onBanned, onClose, }) {
241
256
  }
242
257
  }, disabled: isPending, children: [isPending ? (_jsx(Loader2, { className: "animate-spin", "data-icon": "inline-start" })) : null, m.admin_action_ban] })] })] }) }));
243
258
  }
244
- function UnbanUserDialog({ labels: m, user, onUnbanned, onClose, }) {
259
+ function UnbanUserDialog({ labels: m, authClient, user, onUnbanned, onClose, }) {
245
260
  const [isPending, setIsPending] = useState(false);
246
261
  const [error, setError] = useState("");
247
262
  return (_jsx(AlertDialog, { open: true, onOpenChange: (open) => !open && onClose(), children: _jsxs(AlertDialogContent, { children: [_jsxs(AlertDialogHeader, { children: [_jsx(AlertDialogTitle, { children: m.admin_unban_title }), _jsx(AlertDialogDescription, { children: text(m.admin_unban_description, {
@@ -251,7 +266,9 @@ function UnbanUserDialog({ labels: m, user, onUnbanned, onClose, }) {
251
266
  setError("");
252
267
  setIsPending(true);
253
268
  try {
254
- await unbanUser(user.id, m);
269
+ if (!authClient)
270
+ throw new Error(m.admin_error_access_required);
271
+ await unbanUser(authClient, user.id, m);
255
272
  onUnbanned();
256
273
  onClose();
257
274
  }