@firecms/user_management 3.0.0-canary.41 → 3.0.0-canary.43

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.
@@ -37,8 +37,14 @@ export interface UserManagementParams {
37
37
  */
38
38
  rolesPath?: string;
39
39
 
40
+ /**
41
+ * Maximum number of users that can be created.
42
+ */
40
43
  usersLimit?: number;
41
44
 
45
+ /**
46
+ * Can the logged user edit roles
47
+ */
42
48
  canEditRoles?: boolean;
43
49
 
44
50
  /**
@@ -87,15 +93,6 @@ export function useFirestoreUserManagement({
87
93
 
88
94
  const loading = rolesLoading || usersLoading;
89
95
 
90
- console.log("loading", {
91
- rolesLoading,
92
- usersLoading,
93
- roles,
94
- usersWithRoleIds,
95
- users
96
-
97
- });
98
-
99
96
  useEffect(() => {
100
97
  if (!firebaseApp || !rolesPath) return;
101
98
  const firestore = getFirestore(firebaseApp);
@@ -108,12 +105,13 @@ export function useFirestoreUserManagement({
108
105
  const newRoles = docsToRoles(snapshot.docs);
109
106
  setRoles(newRoles);
110
107
  } catch (e) {
111
- // console.error(e);
108
+ console.error("Error loading roles", e);
112
109
  setRolesError(e as Error);
113
110
  }
114
111
  setRolesLoading(false);
115
112
  },
116
113
  error: (e) => {
114
+ console.error("Error loading roles", e);
117
115
  setRolesError(e);
118
116
  setRolesLoading(false);
119
117
  }
@@ -133,11 +131,13 @@ export function useFirestoreUserManagement({
133
131
  const newUsers = docsToUsers(snapshot.docs);
134
132
  setUsersWithRoleIds(newUsers);
135
133
  } catch (e) {
134
+ console.error("Error loading users", e);
136
135
  setUsersError(e as Error);
137
136
  }
138
137
  setUsersLoading(false);
139
138
  },
140
139
  error: (e) => {
140
+ console.error("Error loading users", e);
141
141
  setUsersError(e);
142
142
  setUsersLoading(false);
143
143
  }
@@ -213,12 +213,10 @@ export function useFirestoreUserManagement({
213
213
  }, [users]);
214
214
 
215
215
  const authenticator: Authenticator = useCallback(({ user }) => {
216
- console.log("Authenticating user", user);
217
- // return true;"
216
+ console.debug("Authenticating user", user);
218
217
 
219
- // console.log("authentication", user, userManagement);
220
218
  if (loading) {
221
- console.log("User management is still loading");
219
+ console.warn("User management is still loading");
222
220
  return false;
223
221
  }
224
222
 
@@ -229,11 +227,10 @@ export function useFirestoreUserManagement({
229
227
 
230
228
  const mgmtUser = users.find(u => u.email?.toLowerCase() === user?.email?.toLowerCase());
231
229
  if (mgmtUser) {
232
- // authController.setRoles(mgmtUser.roles ?? [])
233
230
  return true;
234
231
  }
235
232
 
236
- throw Error("Could not find a user with the provided email");
233
+ throw Error("Could not find a user with the provided email in the user management system.");
237
234
  }, [loading, users])
238
235
 
239
236
  return {
@@ -242,9 +239,11 @@ export function useFirestoreUserManagement({
242
239
  users,
243
240
  saveUser,
244
241
  saveRole,
242
+ rolesError,
245
243
  deleteUser,
246
244
  deleteRole,
247
245
  usersLimit,
246
+ usersError,
248
247
  canEditRoles: canEditRoles === undefined ? true : canEditRoles,
249
248
  allowDefaultRolesCreation: allowDefaultRolesCreation === undefined ? true : allowDefaultRolesCreation,
250
249
  includeCollectionConfigPermissions: Boolean(includeCollectionConfigPermissions),
@@ -42,7 +42,7 @@ export type UserManagement<USER extends User = User> = {
42
42
  * Define the roles for a given user. You will typically want to plug this into your auth controller.
43
43
  * @param user
44
44
  */
45
- defineRolesFor: (user: User) => Promise<Role[]> | Role[] | undefined;
45
+ defineRolesFor: (user: User) => Promise<Role[] | undefined> | Role[] | undefined;
46
46
 
47
47
  /**
48
48
  * You can build an authenticator callback from the current configuration of the user management.
@@ -50,4 +50,7 @@ export type UserManagement<USER extends User = User> = {
50
50
  */
51
51
  authenticator?: Authenticator;
52
52
 
53
+ rolesError?: Error;
54
+ usersError?: Error;
55
+
53
56
  };
@@ -1,13 +1,28 @@
1
- import { FireCMSPlugin } from "@firecms/core";
1
+ import { FireCMSPlugin, useAuthController, useSnackbarController } from "@firecms/core";
2
2
  import { UserManagementProvider } from "./UserManagementProvider";
3
- import { UserManagement } from "./types";
3
+ import { PersistedUser, UserManagement } from "./types";
4
+ import { AddIcon, Button, Paper, Typography } from "@firecms/ui";
5
+ import { DEFAULT_ROLES } from "./components/roles/default_roles";
4
6
 
5
7
  export function useUserManagementPlugin({ userManagement }: {
6
8
  userManagement: UserManagement,
7
9
  }): FireCMSPlugin {
10
+
11
+ const noUsers = userManagement.users.length === 0;
12
+ const noRoles = userManagement.roles.length === 0;
13
+
8
14
  return {
9
15
  key: "user_management",
10
16
  loading: userManagement.loading,
17
+
18
+ homePage: {
19
+ additionalChildrenStart: noUsers || noRoles
20
+ ? <IntroWidget
21
+ noUsers={noUsers}
22
+ noRoles={noRoles}
23
+ userManagement={userManagement}/>
24
+ : undefined
25
+ },
11
26
  provider: {
12
27
  Component: UserManagementProvider,
13
28
  props: {
@@ -16,3 +31,74 @@ export function useUserManagementPlugin({ userManagement }: {
16
31
  }
17
32
  }
18
33
  }
34
+
35
+ export function IntroWidget({
36
+ noUsers,
37
+ noRoles,
38
+ userManagement
39
+ }: {
40
+ noUsers: boolean;
41
+ noRoles: boolean;
42
+ userManagement: UserManagement<PersistedUser>;
43
+ }) {
44
+
45
+ const authController = useAuthController();
46
+ const snackbarController = useSnackbarController();
47
+
48
+ const buttonLabel = noUsers && noRoles
49
+ ? "Create default roles and add current user as admin"
50
+ : noUsers
51
+ ? "Add current user as admin"
52
+ : noRoles ? "Create default roles" : undefined;
53
+
54
+ return (
55
+ <Paper
56
+ className={"my-4 flex flex-col px-4 py-6 bg-white dark:bg-slate-800 gap-2"}>
57
+ <Typography variant={"subtitle2"} className={"uppercase"}>Create your users and roles</Typography>
58
+ <Typography>
59
+ You have no users or roles defined. You can create default roles and add the current user as admin.
60
+ </Typography>
61
+ <Button onClick={() => {
62
+ if (!authController.user?.uid) {
63
+ throw Error("UsersTable, authController misconfiguration");
64
+ }
65
+ if (noUsers) {
66
+ userManagement.saveUser({
67
+ uid: authController.user?.uid,
68
+ email: authController.user?.email,
69
+ displayName: authController.user?.displayName,
70
+ photoURL: authController.user?.photoURL,
71
+ providerId: authController.user?.providerId,
72
+ isAnonymous: authController.user?.isAnonymous,
73
+ roles: [{
74
+ id: "admin",
75
+ name: "Admin"
76
+ }],
77
+ created_on: new Date()
78
+ })
79
+ .then(() => {
80
+ snackbarController.open({
81
+ type: "success",
82
+ message: "User added successfully"
83
+ })
84
+ })
85
+ .catch((error) => {
86
+ snackbarController.open({
87
+ type: "error",
88
+ message: "Error adding user: " + error.message
89
+ })
90
+ });
91
+ }
92
+ if (noRoles) {
93
+ DEFAULT_ROLES.forEach((role) => {
94
+ userManagement.saveRole(role);
95
+ });
96
+ }
97
+ }}>
98
+ <AddIcon/>
99
+ {buttonLabel}
100
+ </Button>
101
+ </Paper>
102
+ );
103
+
104
+ }