@firecms/user_management 3.0.0-canary.143 → 3.0.0-canary.145

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/index.umd.js CHANGED
@@ -155,6 +155,7 @@
155
155
  return `rgba(${r}, ${g}, ${b}, ${alpha})`;
156
156
  }
157
157
  function useBuildUserManagement({
158
+ authController,
158
159
  dataSourceDelegate,
159
160
  usersPath = "__FIRECMS/config/users",
160
161
  rolesPath = "__FIRECMS/config/roles",
@@ -173,14 +174,13 @@
173
174
  }));
174
175
  const [rolesError, setRolesError] = React.useState();
175
176
  const [usersError, setUsersError] = React.useState();
176
- const loading = rolesLoading || usersLoading;
177
+ const _usersLoading = usersLoading;
178
+ const _rolesLoading = rolesLoading;
179
+ const loading = _rolesLoading || _usersLoading;
177
180
  React.useEffect(() => {
178
181
  if (!dataSourceDelegate || !rolesPath) return;
179
182
  if (dataSourceDelegate.initialised !== void 0 && !dataSourceDelegate.initialised) return;
180
- if (dataSourceDelegate.authenticated !== void 0 && !dataSourceDelegate.authenticated) {
181
- setRolesLoading(false);
182
- return;
183
- }
183
+ if (authController?.initialLoading) return;
184
184
  setRolesLoading(true);
185
185
  return dataSourceDelegate.listenCollection?.({
186
186
  path: rolesPath,
@@ -188,8 +188,9 @@
188
188
  setRolesError(void 0);
189
189
  try {
190
190
  const newRoles = entityToRoles(entities);
191
- if (!equal(newRoles, roles))
191
+ if (!equal(newRoles, roles)) {
192
192
  setRoles(newRoles);
193
+ }
193
194
  } catch (e) {
194
195
  setRoles([]);
195
196
  console.error("Error loading roles", e);
@@ -204,23 +205,24 @@
204
205
  setRolesLoading(false);
205
206
  }
206
207
  });
207
- }, [dataSourceDelegate?.initialised, dataSourceDelegate?.authenticated, rolesPath]);
208
+ }, [dataSourceDelegate?.initialised, authController?.initialLoading, authController?.user?.uid, rolesPath]);
208
209
  React.useEffect(() => {
209
210
  if (!dataSourceDelegate || !usersPath) return;
210
- if (dataSourceDelegate.initialised !== void 0 && !dataSourceDelegate.initialised) return;
211
- if (dataSourceDelegate.authenticated !== void 0 && !dataSourceDelegate.authenticated) {
212
- setUsersLoading(false);
211
+ if (dataSourceDelegate.initialised !== void 0 && !dataSourceDelegate.initialised) {
212
+ return;
213
+ }
214
+ if (authController?.initialLoading) {
213
215
  return;
214
216
  }
215
217
  setUsersLoading(true);
216
218
  return dataSourceDelegate.listenCollection?.({
217
219
  path: usersPath,
218
220
  onUpdate(entities) {
221
+ console.debug("Updating users", entities);
219
222
  setUsersError(void 0);
220
223
  try {
221
224
  const newUsers = entitiesToUsers(entities);
222
- if (!equal(newUsers, usersWithRoleIds))
223
- setUsersWithRoleIds(newUsers);
225
+ setUsersWithRoleIds(newUsers);
224
226
  } catch (e) {
225
227
  setUsersWithRoleIds([]);
226
228
  console.error("Error loading users", e);
@@ -229,13 +231,13 @@
229
231
  setUsersLoading(false);
230
232
  },
231
233
  onError(e) {
232
- setUsersWithRoleIds([]);
233
234
  console.error("Error loading users", e);
235
+ setUsersWithRoleIds([]);
234
236
  setUsersError(e);
235
237
  setUsersLoading(false);
236
238
  }
237
239
  });
238
- }, [dataSourceDelegate?.initialised, dataSourceDelegate?.authenticated, usersPath]);
240
+ }, [dataSourceDelegate?.initialised, authController?.initialLoading, authController?.user?.uid, usersPath]);
239
241
  const saveUser = React.useCallback(async (user) => {
240
242
  if (!dataSourceDelegate) throw Error("useBuildUserManagement Firebase not initialised");
241
243
  if (!usersPath) throw Error("useBuildUserManagement Firestore not initialised");
@@ -307,26 +309,36 @@
307
309
  user
308
310
  }), []);
309
311
  const defineRolesFor = React.useCallback((user) => {
310
- if (!users) throw Error("Users not loaded");
311
- const mgmtUser = users.find((u) => u.email?.toLowerCase() === user?.email?.toLowerCase());
312
+ if (!usersWithRoleIds) throw Error("Users not loaded");
313
+ const users2 = usersWithRoleIds.map((u) => ({
314
+ ...u,
315
+ roles: roles.filter((r) => u.roles?.includes(r.id))
316
+ }));
317
+ const mgmtUser = users2.find((u) => u.email?.toLowerCase() === user?.email?.toLowerCase());
312
318
  return mgmtUser?.roles;
313
- }, [users]);
319
+ }, [roles, usersWithRoleIds]);
320
+ console.debug({
321
+ loading,
322
+ users,
323
+ usersError
324
+ });
314
325
  const authenticator = React.useCallback(({ user }) => {
315
- console.debug("Authenticating user", user);
316
326
  if (loading) {
317
- console.warn("User management is still loading");
318
327
  return false;
319
328
  }
320
329
  if (users.length === 0) {
330
+ console.warn("No users created yet");
321
331
  return true;
322
332
  }
323
333
  const mgmtUser = users.find((u) => u.email?.toLowerCase() === user?.email?.toLowerCase());
324
334
  if (mgmtUser) {
335
+ console.debug("User found in user management system", mgmtUser);
325
336
  return true;
326
337
  }
327
338
  throw Error("Could not find a user with the provided email in the user management system.");
328
- }, [loading, users, usersError, rolesError]);
329
- const isAdmin = roles.some((r) => r.id === "admin");
339
+ }, [loading, users]);
340
+ const userRoles = authController.user ? defineRolesFor(authController.user) : void 0;
341
+ const isAdmin = (userRoles ?? []).some((r) => r.id === "admin");
330
342
  return {
331
343
  loading,
332
344
  roles,
@@ -344,7 +356,14 @@
344
356
  includeCollectionConfigPermissions: Boolean(includeCollectionConfigPermissions),
345
357
  collectionPermissions,
346
358
  defineRolesFor,
347
- authenticator
359
+ authenticator,
360
+ ...authController,
361
+ initialLoading: authController.initialLoading || loading,
362
+ userRoles,
363
+ user: authController.user ? {
364
+ ...authController.user,
365
+ roles: userRoles
366
+ } : null
348
367
  };
349
368
  }
350
369
  const entitiesToUsers = (docs) => {
@@ -399,6 +418,7 @@
399
418
  });
400
419
  function canRoleBeEdited(loggedUser) {
401
420
  const loggedUserIsAdmin = loggedUser.roles?.map((r) => r.id).includes("admin");
421
+ console.log("loggedUserIsAdmin", loggedUser);
402
422
  if (!loggedUserIsAdmin) {
403
423
  throw new Error("Only admins can edit roles");
404
424
  }
@@ -1341,7 +1361,6 @@
1341
1361
  /* @__PURE__ */ jsxRuntime.jsxs(ui.Table, { className: "w-full", children: [
1342
1362
  /* @__PURE__ */ jsxRuntime.jsxs(ui.TableHeader, { children: [
1343
1363
  /* @__PURE__ */ jsxRuntime.jsx(ui.TableCell, { className: "truncate w-16" }),
1344
- /* @__PURE__ */ jsxRuntime.jsx(ui.TableCell, { children: "ID" }),
1345
1364
  /* @__PURE__ */ jsxRuntime.jsx(ui.TableCell, { children: "Email" }),
1346
1365
  /* @__PURE__ */ jsxRuntime.jsx(ui.TableCell, { children: "Name" }),
1347
1366
  /* @__PURE__ */ jsxRuntime.jsx(ui.TableCell, { children: "Roles" }),
@@ -1376,7 +1395,6 @@
1376
1395
  )
1377
1396
  }
1378
1397
  ) }),
1379
- /* @__PURE__ */ jsxRuntime.jsx(ui.TableCell, { children: user.uid }),
1380
1398
  /* @__PURE__ */ jsxRuntime.jsx(ui.TableCell, { children: user.email }),
1381
1399
  /* @__PURE__ */ jsxRuntime.jsx(ui.TableCell, { className: "font-medium align-left", children: user.displayName }),
1382
1400
  /* @__PURE__ */ jsxRuntime.jsx(ui.TableCell, { className: "align-left", children: userRoles ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-wrap gap-2", children: userRoles.map(
@@ -1544,7 +1562,7 @@
1544
1562
  return /* @__PURE__ */ jsxRuntime.jsxs(
1545
1563
  ui.Paper,
1546
1564
  {
1547
- className: "my-4 flex flex-col px-4 py-6 bg-white dark:bg-slate-800 gap-2",
1565
+ className: "my-4 flex flex-col px-4 py-6 bg-white dark:bg-surface-accent-800 gap-2",
1548
1566
  children: [
1549
1567
  /* @__PURE__ */ jsxRuntime.jsx(ui.Typography, { variant: "subtitle2", className: "uppercase", children: "Create your users and roles" }),
1550
1568
  /* @__PURE__ */ jsxRuntime.jsx(ui.Typography, { children: "You have no users or roles defined. You can create default roles and add the current user as admin." }),