@firecms/user_management 3.0.0-canary.16 → 3.0.0-canary.18

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.
@@ -1,5 +1,6 @@
1
1
  import React, { useCallback, useEffect, useRef } from "react";
2
2
  import {
3
+ addDoc,
3
4
  collection,
4
5
  deleteDoc,
5
6
  doc,
@@ -11,7 +12,7 @@ import {
11
12
  } from "firebase/firestore";
12
13
  import { FirebaseApp } from "firebase/app";
13
14
  import { UserManagement } from "../types";
14
- import { PermissionsBuilder, Role, User } from "@firecms/core";
15
+ import { Authenticator, PermissionsBuilder, Role, User } from "@firecms/core";
15
16
  import { resolveUserRolePermissions } from "../utils";
16
17
 
17
18
  type UserWithRoleIds = User & { roles: string[] };
@@ -152,10 +153,15 @@ export function useBuildFirestoreUserManagement({
152
153
  uid,
153
154
  ...userData
154
155
  } = user;
155
- return setDoc(doc(firestore, usersPath, uid), {
156
+ const data = {
156
157
  ...userData,
157
158
  roles: roleIds
158
- }, { merge: true }).then(() => user);
159
+ };
160
+ if (uid) {
161
+ return setDoc(doc(firestore, usersPath, uid), data, { merge: true }).then(() => user);
162
+ } else {
163
+ return addDoc(collection(firestore, usersPath), data).then(() => user);
164
+ }
159
165
  }, [usersPath]);
160
166
 
161
167
  const saveRole = useCallback((role: Role): Promise<void> => {
@@ -202,6 +208,30 @@ export function useBuildFirestoreUserManagement({
202
208
  return mgmtUser?.roles;
203
209
  }, [userIds]);
204
210
 
211
+ const authenticator: Authenticator = useCallback(({ user }) => {
212
+ console.log("Authenticating user", user);
213
+ // return true;"
214
+
215
+ // console.log("authentication", user, userManagement);
216
+ if (loading) {
217
+ console.log("User management is still loading");
218
+ return false;
219
+ }
220
+
221
+ // This is an example of how you can link the access system to the user management plugin
222
+ if (users.length === 0) {
223
+ return true; // If there are no users created yet, we allow access to every user
224
+ }
225
+
226
+ const mgmtUser = users.find(u => u.email?.toLowerCase() === user?.email?.toLowerCase());
227
+ if (mgmtUser) {
228
+ // authController.setRoles(mgmtUser.roles ?? [])
229
+ return true;
230
+ }
231
+
232
+ throw Error("Could not find a user with the provided email");
233
+ }, [loading, users])
234
+
205
235
  return {
206
236
  loading,
207
237
  roles,
@@ -215,7 +245,8 @@ export function useBuildFirestoreUserManagement({
215
245
  allowDefaultRolesCreation: allowDefaultRolesCreation === undefined ? true : allowDefaultRolesCreation,
216
246
  includeCollectionConfigPermissions: Boolean(includeCollectionConfigPermissions),
217
247
  collectionPermissions,
218
- defineRolesFor
248
+ defineRolesFor,
249
+ authenticator
219
250
  }
220
251
  }
221
252
 
@@ -1,4 +1,4 @@
1
- import { PermissionsBuilder, Role, User } from "@firecms/core";
1
+ import { Authenticator, PermissionsBuilder, Role, User } from "@firecms/core";
2
2
 
3
3
  export type UserManagement<USER extends User = User> = {
4
4
 
@@ -39,9 +39,15 @@ export type UserManagement<USER extends User = User> = {
39
39
  collectionPermissions: PermissionsBuilder;
40
40
 
41
41
  /**
42
- * Define the roles for a given user.
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
45
  defineRolesFor: (user: User) => Promise<Role[]> | Role[] | undefined;
46
46
 
47
+ /**
48
+ * You can build an authenticator callback from the current configuration of the user management.
49
+ * It will only allow access to users with the required roles.
50
+ */
51
+ authenticator?: Authenticator;
52
+
47
53
  };