@firecms/user_management 3.0.0-canary.144 → 3.0.0-canary.146

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",
@@ -163,6 +164,9 @@
163
164
  allowDefaultRolesCreation,
164
165
  includeCollectionConfigPermissions
165
166
  }) {
167
+ if (!authController) {
168
+ throw Error("useBuildUserManagement: You need to provide an authController since version 3.0.0-beta.11. Check https://firecms.co/docs/pro/migrating_from_v3_beta");
169
+ }
166
170
  const [rolesLoading, setRolesLoading] = React.useState(true);
167
171
  const [usersLoading, setUsersLoading] = React.useState(true);
168
172
  const [roles, setRoles] = React.useState([]);
@@ -173,14 +177,13 @@
173
177
  }));
174
178
  const [rolesError, setRolesError] = React.useState();
175
179
  const [usersError, setUsersError] = React.useState();
176
- const loading = rolesLoading || usersLoading;
180
+ const _usersLoading = usersLoading;
181
+ const _rolesLoading = rolesLoading;
182
+ const loading = _rolesLoading || _usersLoading;
177
183
  React.useEffect(() => {
178
184
  if (!dataSourceDelegate || !rolesPath) return;
179
185
  if (dataSourceDelegate.initialised !== void 0 && !dataSourceDelegate.initialised) return;
180
- if (dataSourceDelegate.authenticated !== void 0 && !dataSourceDelegate.authenticated) {
181
- setRolesLoading(false);
182
- return;
183
- }
186
+ if (authController?.initialLoading) return;
184
187
  setRolesLoading(true);
185
188
  return dataSourceDelegate.listenCollection?.({
186
189
  path: rolesPath,
@@ -188,8 +191,9 @@
188
191
  setRolesError(void 0);
189
192
  try {
190
193
  const newRoles = entityToRoles(entities);
191
- if (!equal(newRoles, roles))
194
+ if (!equal(newRoles, roles)) {
192
195
  setRoles(newRoles);
196
+ }
193
197
  } catch (e) {
194
198
  setRoles([]);
195
199
  console.error("Error loading roles", e);
@@ -204,23 +208,24 @@
204
208
  setRolesLoading(false);
205
209
  }
206
210
  });
207
- }, [dataSourceDelegate?.initialised, dataSourceDelegate?.authenticated, rolesPath]);
211
+ }, [dataSourceDelegate?.initialised, authController?.initialLoading, authController?.user?.uid, rolesPath]);
208
212
  React.useEffect(() => {
209
213
  if (!dataSourceDelegate || !usersPath) return;
210
- if (dataSourceDelegate.initialised !== void 0 && !dataSourceDelegate.initialised) return;
211
- if (dataSourceDelegate.authenticated !== void 0 && !dataSourceDelegate.authenticated) {
212
- setUsersLoading(false);
214
+ if (dataSourceDelegate.initialised !== void 0 && !dataSourceDelegate.initialised) {
215
+ return;
216
+ }
217
+ if (authController?.initialLoading) {
213
218
  return;
214
219
  }
215
220
  setUsersLoading(true);
216
221
  return dataSourceDelegate.listenCollection?.({
217
222
  path: usersPath,
218
223
  onUpdate(entities) {
224
+ console.debug("Updating users", entities);
219
225
  setUsersError(void 0);
220
226
  try {
221
227
  const newUsers = entitiesToUsers(entities);
222
- if (!equal(newUsers, usersWithRoleIds))
223
- setUsersWithRoleIds(newUsers);
228
+ setUsersWithRoleIds(newUsers);
224
229
  } catch (e) {
225
230
  setUsersWithRoleIds([]);
226
231
  console.error("Error loading users", e);
@@ -229,13 +234,13 @@
229
234
  setUsersLoading(false);
230
235
  },
231
236
  onError(e) {
232
- setUsersWithRoleIds([]);
233
237
  console.error("Error loading users", e);
238
+ setUsersWithRoleIds([]);
234
239
  setUsersError(e);
235
240
  setUsersLoading(false);
236
241
  }
237
242
  });
238
- }, [dataSourceDelegate?.initialised, dataSourceDelegate?.authenticated, usersPath]);
243
+ }, [dataSourceDelegate?.initialised, authController?.initialLoading, authController?.user?.uid, usersPath]);
239
244
  const saveUser = React.useCallback(async (user) => {
240
245
  if (!dataSourceDelegate) throw Error("useBuildUserManagement Firebase not initialised");
241
246
  if (!usersPath) throw Error("useBuildUserManagement Firestore not initialised");
@@ -307,26 +312,31 @@
307
312
  user
308
313
  }), []);
309
314
  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());
315
+ if (!usersWithRoleIds) throw Error("Users not loaded");
316
+ const users2 = usersWithRoleIds.map((u) => ({
317
+ ...u,
318
+ roles: roles.filter((r) => u.roles?.includes(r.id))
319
+ }));
320
+ const mgmtUser = users2.find((u) => u.email?.toLowerCase() === user?.email?.toLowerCase());
312
321
  return mgmtUser?.roles;
313
- }, [users]);
322
+ }, [roles, usersWithRoleIds]);
314
323
  const authenticator = React.useCallback(({ user }) => {
315
- console.debug("Authenticating user", user);
316
324
  if (loading) {
317
- console.warn("User management is still loading");
318
325
  return false;
319
326
  }
320
327
  if (users.length === 0) {
328
+ console.warn("No users created yet");
321
329
  return true;
322
330
  }
323
331
  const mgmtUser = users.find((u) => u.email?.toLowerCase() === user?.email?.toLowerCase());
324
332
  if (mgmtUser) {
333
+ console.debug("User found in user management system", mgmtUser);
325
334
  return true;
326
335
  }
327
336
  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");
337
+ }, [loading, users]);
338
+ const userRoles = authController.user ? defineRolesFor(authController.user) : void 0;
339
+ const isAdmin = (userRoles ?? []).some((r) => r.id === "admin");
330
340
  return {
331
341
  loading,
332
342
  roles,
@@ -344,7 +354,14 @@
344
354
  includeCollectionConfigPermissions: Boolean(includeCollectionConfigPermissions),
345
355
  collectionPermissions,
346
356
  defineRolesFor,
347
- authenticator
357
+ authenticator,
358
+ ...authController,
359
+ initialLoading: authController.initialLoading || loading,
360
+ userRoles,
361
+ user: authController.user ? {
362
+ ...authController.user,
363
+ roles: userRoles
364
+ } : null
348
365
  };
349
366
  }
350
367
  const entitiesToUsers = (docs) => {
@@ -399,6 +416,7 @@
399
416
  });
400
417
  function canRoleBeEdited(loggedUser) {
401
418
  const loggedUserIsAdmin = loggedUser.roles?.map((r) => r.id).includes("admin");
419
+ console.log("loggedUserIsAdmin", loggedUser);
402
420
  if (!loggedUserIsAdmin) {
403
421
  throw new Error("Only admins can edit roles");
404
422
  }
@@ -1341,7 +1359,6 @@
1341
1359
  /* @__PURE__ */ jsxRuntime.jsxs(ui.Table, { className: "w-full", children: [
1342
1360
  /* @__PURE__ */ jsxRuntime.jsxs(ui.TableHeader, { children: [
1343
1361
  /* @__PURE__ */ jsxRuntime.jsx(ui.TableCell, { className: "truncate w-16" }),
1344
- /* @__PURE__ */ jsxRuntime.jsx(ui.TableCell, { children: "ID" }),
1345
1362
  /* @__PURE__ */ jsxRuntime.jsx(ui.TableCell, { children: "Email" }),
1346
1363
  /* @__PURE__ */ jsxRuntime.jsx(ui.TableCell, { children: "Name" }),
1347
1364
  /* @__PURE__ */ jsxRuntime.jsx(ui.TableCell, { children: "Roles" }),
@@ -1376,7 +1393,6 @@
1376
1393
  )
1377
1394
  }
1378
1395
  ) }),
1379
- /* @__PURE__ */ jsxRuntime.jsx(ui.TableCell, { children: user.uid }),
1380
1396
  /* @__PURE__ */ jsxRuntime.jsx(ui.TableCell, { children: user.email }),
1381
1397
  /* @__PURE__ */ jsxRuntime.jsx(ui.TableCell, { className: "font-medium align-left", children: user.displayName }),
1382
1398
  /* @__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 +1560,7 @@
1544
1560
  return /* @__PURE__ */ jsxRuntime.jsxs(
1545
1561
  ui.Paper,
1546
1562
  {
1547
- className: "my-4 flex flex-col px-4 py-6 bg-white dark:bg-slate-800 gap-2",
1563
+ className: "my-4 flex flex-col px-4 py-6 bg-white dark:bg-surface-accent-800 gap-2",
1548
1564
  children: [
1549
1565
  /* @__PURE__ */ jsxRuntime.jsx(ui.Typography, { variant: "subtitle2", className: "uppercase", children: "Create your users and roles" }),
1550
1566
  /* @__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." }),