@rebasepro/client-firebase 0.0.1-canary.09e5ec5
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/LICENSE +21 -0
- package/README.md +4 -0
- package/dist/components/FirebaseLoginView.d.ts +72 -0
- package/dist/components/RebaseFirebaseApp.d.ts +19 -0
- package/dist/components/RebaseFirebaseAppProps.d.ts +144 -0
- package/dist/components/index.d.ts +3 -0
- package/dist/components/social_icons.d.ts +6 -0
- package/dist/hooks/index.d.ts +8 -0
- package/dist/hooks/useAppCheck.d.ts +20 -0
- package/dist/hooks/useBuildUserManagement.d.ts +46 -0
- package/dist/hooks/useFirebaseAuthController.d.ts +15 -0
- package/dist/hooks/useFirebaseRealTimeDBDelegate.d.ts +5 -0
- package/dist/hooks/useFirebaseStorageSource.d.ts +14 -0
- package/dist/hooks/useFirestoreDriver.d.ts +56 -0
- package/dist/hooks/useInitialiseFirebase.d.ts +34 -0
- package/dist/hooks/useRecaptcha.d.ts +8 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.es.js +3060 -0
- package/dist/index.es.js.map +1 -0
- package/dist/index.umd.js +3043 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/social_icons.d.ts +6 -0
- package/dist/types/appcheck.d.ts +10 -0
- package/dist/types/auth.d.ts +41 -0
- package/dist/types/index.d.ts +3 -0
- package/dist/types/text_search.d.ts +39 -0
- package/dist/utils/algolia.d.ts +9 -0
- package/dist/utils/collections_firestore.d.ts +5 -0
- package/dist/utils/database.d.ts +2 -0
- package/dist/utils/index.d.ts +7 -0
- package/dist/utils/local_text_search_controller.d.ts +2 -0
- package/dist/utils/pinecone.d.ts +24 -0
- package/dist/utils/rebase_search_controller.d.ts +73 -0
- package/dist/utils/text_search_controller.d.ts +13 -0
- package/package.json +63 -0
- package/src/components/FirebaseLoginView.tsx +693 -0
- package/src/components/RebaseFirebaseApp.tsx +291 -0
- package/src/components/RebaseFirebaseAppProps.tsx +180 -0
- package/src/components/index.ts +3 -0
- package/src/components/social_icons.tsx +135 -0
- package/src/hooks/index.ts +8 -0
- package/src/hooks/useAppCheck.ts +101 -0
- package/src/hooks/useBuildUserManagement.tsx +374 -0
- package/src/hooks/useFirebaseAuthController.ts +334 -0
- package/src/hooks/useFirebaseRealTimeDBDelegate.ts +269 -0
- package/src/hooks/useFirebaseStorageSource.ts +207 -0
- package/src/hooks/useFirestoreDriver.ts +784 -0
- package/src/hooks/useInitialiseFirebase.ts +132 -0
- package/src/hooks/useRecaptcha.tsx +28 -0
- package/src/index.ts +4 -0
- package/src/social_icons.tsx +135 -0
- package/src/types/appcheck.ts +11 -0
- package/src/types/auth.tsx +74 -0
- package/src/types/index.ts +3 -0
- package/src/types/text_search.ts +42 -0
- package/src/utils/algolia.ts +27 -0
- package/src/utils/collections_firestore.ts +148 -0
- package/src/utils/database.ts +39 -0
- package/src/utils/index.ts +7 -0
- package/src/utils/local_text_search_controller.ts +143 -0
- package/src/utils/pinecone.ts +75 -0
- package/src/utils/rebase_search_controller.ts +357 -0
- package/src/utils/text_search_controller.ts +34 -0
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import React, { useCallback, useEffect, useRef } from "react";
|
|
2
|
+
|
|
3
|
+
import { AppCheck, getToken, initializeAppCheck } from "@firebase/app-check";
|
|
4
|
+
import { FirebaseApp } from "@firebase/app";
|
|
5
|
+
import { AppCheckOptions } from "../types";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @group Firebase
|
|
9
|
+
*/
|
|
10
|
+
export interface InitializeAppCheckProps {
|
|
11
|
+
firebaseApp?: FirebaseApp;
|
|
12
|
+
options?: AppCheckOptions;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface InitializeAppCheckResult {
|
|
16
|
+
loading: boolean;
|
|
17
|
+
appCheckVerified?: boolean;
|
|
18
|
+
error?: any;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Function used to initialise Firebase App Check.
|
|
23
|
+
*
|
|
24
|
+
* @group Firebase
|
|
25
|
+
*/
|
|
26
|
+
export function useAppCheck({
|
|
27
|
+
firebaseApp,
|
|
28
|
+
options
|
|
29
|
+
}: InitializeAppCheckProps): InitializeAppCheckResult {
|
|
30
|
+
if (options?.debugToken) {
|
|
31
|
+
Object.assign(window, {
|
|
32
|
+
FIREBASE_APPCHECK_DEBUG_TOKEN: options?.debugToken
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const [appCheckLoading, setAppCheckLoading] = React.useState<boolean>(false);
|
|
37
|
+
const [appCheckVerified, setAppCheckVerified] = React.useState<boolean | undefined>(undefined);
|
|
38
|
+
const [error, setError] = React.useState<any>();
|
|
39
|
+
|
|
40
|
+
const initialCheck = useRef<boolean>(false);
|
|
41
|
+
|
|
42
|
+
const verifyToken = useCallback(async (appCheck: AppCheck) => {
|
|
43
|
+
console.debug("Verifying App Check token...", appCheck);
|
|
44
|
+
try {
|
|
45
|
+
const token = await getToken(appCheck, options?.forceRefresh);
|
|
46
|
+
console.debug("App Check token:", token);
|
|
47
|
+
if (!token) {
|
|
48
|
+
setError("App Check failed.");
|
|
49
|
+
setAppCheckVerified(false);
|
|
50
|
+
} else {
|
|
51
|
+
setAppCheckVerified(true);
|
|
52
|
+
console.debug("App Check success.");
|
|
53
|
+
}
|
|
54
|
+
} catch (e: any) {
|
|
55
|
+
console.error("App Check error:", e);
|
|
56
|
+
setError(e.message);
|
|
57
|
+
}
|
|
58
|
+
}, [options?.forceRefresh]);
|
|
59
|
+
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
if (!options) return;
|
|
62
|
+
if (!firebaseApp) return;
|
|
63
|
+
if (appCheckVerified !== undefined) return;
|
|
64
|
+
if (initialCheck.current) return;
|
|
65
|
+
|
|
66
|
+
setAppCheckLoading(true);
|
|
67
|
+
|
|
68
|
+
const {
|
|
69
|
+
provider,
|
|
70
|
+
isTokenAutoRefreshEnabled
|
|
71
|
+
} = options;
|
|
72
|
+
|
|
73
|
+
removeCurrentAppCheckDiv();
|
|
74
|
+
|
|
75
|
+
const appCheck = initializeAppCheck(firebaseApp, {
|
|
76
|
+
provider,
|
|
77
|
+
isTokenAutoRefreshEnabled
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
verifyToken(appCheck)
|
|
81
|
+
.then(() => {
|
|
82
|
+
setAppCheckLoading(false);
|
|
83
|
+
});
|
|
84
|
+
initialCheck.current = true;
|
|
85
|
+
}, [appCheckVerified, firebaseApp, options, verifyToken]);
|
|
86
|
+
|
|
87
|
+
return {
|
|
88
|
+
loading: appCheckLoading,
|
|
89
|
+
appCheckVerified,
|
|
90
|
+
error
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
function removeCurrentAppCheckDiv() {
|
|
96
|
+
const div = document.getElementById("fire_app_check_[DEFAULT]");
|
|
97
|
+
if (div) {
|
|
98
|
+
div.remove();
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
import React, { useCallback, useEffect } from "react";
|
|
2
|
+
import equal from "react-fast-compare"
|
|
3
|
+
|
|
4
|
+
import { removeUndefined } from "@rebasepro/utils";
|
|
5
|
+
import {
|
|
6
|
+
AuthController,
|
|
7
|
+
Authenticator,
|
|
8
|
+
DataDriver,
|
|
9
|
+
Entity,
|
|
10
|
+
Role,
|
|
11
|
+
User,
|
|
12
|
+
UserManagementDelegate
|
|
13
|
+
} from "@rebasepro/types";
|
|
14
|
+
|
|
15
|
+
type UserWithRoleIds<USER extends User = any> = Omit<USER, "roles"> & { roles: string[] };
|
|
16
|
+
|
|
17
|
+
export interface UserManagementDelegateParams<CONTROLLER extends AuthController<any> = AuthController<any>> {
|
|
18
|
+
|
|
19
|
+
authController: CONTROLLER;
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The delegate in charge of persisting the data.
|
|
23
|
+
*/
|
|
24
|
+
dataSourceDelegate?: DataDriver;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Path where the plugin users configuration is stored.
|
|
28
|
+
* Default: __FIRECMS/config/users
|
|
29
|
+
* You can specify a different path if you want to store the user management configuration in a different place.
|
|
30
|
+
* Please keep in mind that the FireCMS users are not necessarily the same as the Firebase users (but they can be).
|
|
31
|
+
* The path should be relative to the root of the database, and should always have an odd number of segments.
|
|
32
|
+
*/
|
|
33
|
+
usersPath?: string;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Path where the plugin roles configuration is stored.
|
|
37
|
+
* Default: __FIRECMS/config/roles
|
|
38
|
+
*/
|
|
39
|
+
rolesPath?: string;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* The roles that are available in the user management system.
|
|
43
|
+
* If you provide this, the user management system will not fetch the roles from the database.
|
|
44
|
+
*/
|
|
45
|
+
roles?: Role[];
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* If there are no roles in the database, provide a button to create the default roles.
|
|
49
|
+
*/
|
|
50
|
+
allowDefaultRolesCreation?: boolean;
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Include the collection config permissions in the user management system.
|
|
54
|
+
*/
|
|
55
|
+
includeCollectionConfigPermissions?: boolean;
|
|
56
|
+
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* This hook is used to build a user management object that can be used to
|
|
61
|
+
* manage users and roles in a Firestore backend.
|
|
62
|
+
* @param authController
|
|
63
|
+
* @param dataSourceDelegate
|
|
64
|
+
* @param usersPath
|
|
65
|
+
* @param rolesPath
|
|
66
|
+
* @param roles
|
|
67
|
+
* @param allowDefaultRolesCreation
|
|
68
|
+
* @param includeCollectionConfigPermissions
|
|
69
|
+
*/
|
|
70
|
+
export function useBuildUserManagement<CONTROLLER extends AuthController<any> = AuthController<any>,
|
|
71
|
+
USER extends User = CONTROLLER extends AuthController<infer U> ? U : any>
|
|
72
|
+
({
|
|
73
|
+
authController,
|
|
74
|
+
dataSourceDelegate,
|
|
75
|
+
roles: rolesProp,
|
|
76
|
+
usersPath = "__FIRECMS/config/users",
|
|
77
|
+
rolesPath = "__FIRECMS/config/roles",
|
|
78
|
+
allowDefaultRolesCreation,
|
|
79
|
+
includeCollectionConfigPermissions
|
|
80
|
+
}: UserManagementDelegateParams<CONTROLLER>): UserManagementDelegate<USER> & CONTROLLER {
|
|
81
|
+
|
|
82
|
+
if (!authController) {
|
|
83
|
+
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");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const rolesDefinedInCode = (rolesProp ?? [])?.length > 0;
|
|
87
|
+
const [rolesLoading, setRolesLoading] = React.useState<boolean>(!rolesDefinedInCode);
|
|
88
|
+
const [usersLoading, setUsersLoading] = React.useState<boolean>(true);
|
|
89
|
+
const [roles, setRoles] = React.useState<Role[]>(rolesProp ?? []);
|
|
90
|
+
const [usersWithRoleIds, setUsersWithRoleIds] = React.useState<UserWithRoleIds<USER>[]>([]);
|
|
91
|
+
|
|
92
|
+
const users = usersWithRoleIds.map(u => ({
|
|
93
|
+
...u,
|
|
94
|
+
}) as unknown as USER);
|
|
95
|
+
|
|
96
|
+
const [rolesError, setRolesError] = React.useState<Error | undefined>();
|
|
97
|
+
const [usersError, setUsersError] = React.useState<Error | undefined>();
|
|
98
|
+
|
|
99
|
+
const _usersLoading = usersLoading;
|
|
100
|
+
const _rolesLoading = rolesLoading;
|
|
101
|
+
|
|
102
|
+
const loading = _rolesLoading || _usersLoading;
|
|
103
|
+
|
|
104
|
+
useEffect(() => {
|
|
105
|
+
if (rolesDefinedInCode) return;
|
|
106
|
+
if (!dataSourceDelegate || !rolesPath) return;
|
|
107
|
+
if (dataSourceDelegate.initialised !== undefined && !dataSourceDelegate.initialised) return;
|
|
108
|
+
if (authController?.initialLoading) return;
|
|
109
|
+
|
|
110
|
+
setRolesLoading(true);
|
|
111
|
+
return dataSourceDelegate.listenCollection?.({
|
|
112
|
+
path: rolesPath,
|
|
113
|
+
onUpdate(entities: Entity<any>[]): void {
|
|
114
|
+
setRolesError(undefined);
|
|
115
|
+
console.debug("Updating roles", entities);
|
|
116
|
+
try {
|
|
117
|
+
const newRoles = entityToRoles(entities);
|
|
118
|
+
if (!equal(newRoles, roles)) {
|
|
119
|
+
setRoles(newRoles);
|
|
120
|
+
}
|
|
121
|
+
} catch (e) {
|
|
122
|
+
setRoles([]);
|
|
123
|
+
console.error("Error loading roles", e);
|
|
124
|
+
setRolesError(e as Error);
|
|
125
|
+
}
|
|
126
|
+
setRolesLoading(false);
|
|
127
|
+
},
|
|
128
|
+
onError(e: any): void {
|
|
129
|
+
setRoles([]);
|
|
130
|
+
console.error("Error loading roles", e);
|
|
131
|
+
setRolesError(e);
|
|
132
|
+
setRolesLoading(false);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
}, [rolesDefinedInCode, dataSourceDelegate?.initialised, authController?.initialLoading, authController?.user?.uid, rolesPath]);
|
|
137
|
+
|
|
138
|
+
useEffect(() => {
|
|
139
|
+
if (!dataSourceDelegate || !usersPath) return;
|
|
140
|
+
if (dataSourceDelegate.initialised !== undefined && !dataSourceDelegate.initialised) {
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
if (authController?.initialLoading) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
setUsersLoading(true);
|
|
148
|
+
return dataSourceDelegate.listenCollection?.({
|
|
149
|
+
path: usersPath,
|
|
150
|
+
onUpdate(entities: Entity<any>[]): void {
|
|
151
|
+
console.debug("Updating users", entities);
|
|
152
|
+
setUsersError(undefined);
|
|
153
|
+
try {
|
|
154
|
+
const newUsers = entitiesToUsers(entities);
|
|
155
|
+
// if (!equal(newUsers, usersWithRoleIds))
|
|
156
|
+
setUsersWithRoleIds(newUsers);
|
|
157
|
+
} catch (e) {
|
|
158
|
+
setUsersWithRoleIds([]);
|
|
159
|
+
console.error("Error loading users", e);
|
|
160
|
+
setUsersError(e as Error);
|
|
161
|
+
}
|
|
162
|
+
setUsersLoading(false);
|
|
163
|
+
},
|
|
164
|
+
onError(e: any): void {
|
|
165
|
+
console.error("Error loading users", e);
|
|
166
|
+
setUsersWithRoleIds([]);
|
|
167
|
+
setUsersError(e);
|
|
168
|
+
setUsersLoading(false);
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
}, [dataSourceDelegate?.initialised, authController?.initialLoading, authController?.user?.uid, usersPath]);
|
|
173
|
+
|
|
174
|
+
const saveUser = useCallback(async (user: USER): Promise<USER> => {
|
|
175
|
+
if (!dataSourceDelegate) throw Error("useBuildUserManagement Firebase not initialised");
|
|
176
|
+
if (!usersPath) throw Error("useBuildUserManagement Firestore not initialised");
|
|
177
|
+
|
|
178
|
+
console.debug("Persisting user", user);
|
|
179
|
+
|
|
180
|
+
const roleIds = user.roles;
|
|
181
|
+
const email = user.email?.toLowerCase().trim();
|
|
182
|
+
if (!email) throw Error("Email is required");
|
|
183
|
+
|
|
184
|
+
const userExists = users.find(u => u.email?.toLowerCase() === email);
|
|
185
|
+
const data = {
|
|
186
|
+
...user,
|
|
187
|
+
roles: roleIds ?? []
|
|
188
|
+
};
|
|
189
|
+
if (!userExists) {
|
|
190
|
+
// @ts-ignore
|
|
191
|
+
data.created_on = new Date();
|
|
192
|
+
}
|
|
193
|
+
// delete the previous user entry if it exists and the uid has changed
|
|
194
|
+
if (userExists && userExists.uid !== user.uid) {
|
|
195
|
+
const entity: Entity<any> = {
|
|
196
|
+
values: {},
|
|
197
|
+
path: usersPath,
|
|
198
|
+
id: userExists.uid
|
|
199
|
+
}
|
|
200
|
+
await dataSourceDelegate.deleteEntity({ entity })
|
|
201
|
+
.then(() => {
|
|
202
|
+
console.debug("Deleted previous user", userExists);
|
|
203
|
+
})
|
|
204
|
+
.catch(e => {
|
|
205
|
+
console.error("Error deleting user", e);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
return dataSourceDelegate.saveEntity({
|
|
211
|
+
status: "existing",
|
|
212
|
+
path: usersPath,
|
|
213
|
+
entityId: email,
|
|
214
|
+
values: removeUndefined(data) as Record<string, unknown>
|
|
215
|
+
}).then(() => user);
|
|
216
|
+
}, [usersPath, dataSourceDelegate?.initialised]);
|
|
217
|
+
|
|
218
|
+
const saveRole = useCallback((role: Role): Promise<void> => {
|
|
219
|
+
if (!dataSourceDelegate) throw Error("useBuildUserManagement Firebase not initialised");
|
|
220
|
+
if (!rolesPath) throw Error("useBuildUserManagement Firestore not initialised");
|
|
221
|
+
console.debug("Persisting role", role);
|
|
222
|
+
const {
|
|
223
|
+
id,
|
|
224
|
+
...roleData
|
|
225
|
+
} = role;
|
|
226
|
+
return dataSourceDelegate.saveEntity({
|
|
227
|
+
status: "existing",
|
|
228
|
+
path: rolesPath,
|
|
229
|
+
entityId: id,
|
|
230
|
+
values: removeUndefined(roleData) as Record<string, unknown>
|
|
231
|
+
}).then(() => {
|
|
232
|
+
return;
|
|
233
|
+
});
|
|
234
|
+
}, [rolesPath, dataSourceDelegate?.initialised]);
|
|
235
|
+
|
|
236
|
+
const deleteUser = useCallback(async (user: User): Promise<void> => {
|
|
237
|
+
if (!dataSourceDelegate) throw Error("useBuildUserManagement Firebase not initialised");
|
|
238
|
+
if (!usersPath) throw Error("useBuildUserManagement Firestore not initialised");
|
|
239
|
+
console.debug("Deleting", user);
|
|
240
|
+
const { uid } = user;
|
|
241
|
+
const entity: Entity<any> = {
|
|
242
|
+
path: usersPath,
|
|
243
|
+
id: uid,
|
|
244
|
+
values: {}
|
|
245
|
+
};
|
|
246
|
+
await dataSourceDelegate.deleteEntity({ entity })
|
|
247
|
+
}, [usersPath, dataSourceDelegate?.initialised]);
|
|
248
|
+
|
|
249
|
+
const deleteRole = useCallback(async (role: Role): Promise<void> => {
|
|
250
|
+
if (!dataSourceDelegate) throw Error("useBuildUserManagement Firebase not initialised");
|
|
251
|
+
if (!rolesPath) throw Error("useBuildUserManagement Firestore not initialised");
|
|
252
|
+
console.debug("Deleting", role);
|
|
253
|
+
const { id } = role;
|
|
254
|
+
const entity: Entity<any> = {
|
|
255
|
+
path: rolesPath,
|
|
256
|
+
id: id,
|
|
257
|
+
values: {}
|
|
258
|
+
};
|
|
259
|
+
await dataSourceDelegate.deleteEntity({ entity })
|
|
260
|
+
}, [rolesPath, dataSourceDelegate?.initialised]);
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
|
|
264
|
+
const defineRolesFor: ((user: User) => Role[] | undefined) = useCallback((user) => {
|
|
265
|
+
if (!usersWithRoleIds) throw Error("Users not loaded");
|
|
266
|
+
const mgmtUser = usersWithRoleIds.find(u => u.email?.toLowerCase() === user?.email?.toLowerCase());
|
|
267
|
+
if (!mgmtUser || !mgmtUser.roles) return undefined;
|
|
268
|
+
return roles.filter(r => mgmtUser.roles.includes(r.id));
|
|
269
|
+
}, [roles, usersWithRoleIds]);
|
|
270
|
+
|
|
271
|
+
const authenticator: Authenticator<USER> = useCallback(({ user }) => {
|
|
272
|
+
|
|
273
|
+
if (loading) {
|
|
274
|
+
return false;
|
|
275
|
+
}
|
|
276
|
+
if (user === null) {
|
|
277
|
+
console.warn("User is null, returning");
|
|
278
|
+
return false;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
if (users.length === 0) {
|
|
282
|
+
console.warn("No users created yet");
|
|
283
|
+
return true; // If there are no users created yet, we allow access to every user
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const mgmtUser = users.find(u => u.email?.toLowerCase() === user?.email?.toLowerCase());
|
|
287
|
+
if (mgmtUser) {
|
|
288
|
+
// check if the uid or photoURL needs to be updated in the user management system
|
|
289
|
+
const needsUidUpdate = mgmtUser.uid !== user.uid;
|
|
290
|
+
const needsPhotoUpdate = user.photoURL && mgmtUser.photoURL !== user.photoURL;
|
|
291
|
+
|
|
292
|
+
if (needsUidUpdate || needsPhotoUpdate) {
|
|
293
|
+
const updateReason = needsUidUpdate ? "uid" : "photoURL";
|
|
294
|
+
console.debug(`User ${updateReason} has changed, updating user in user management system`);
|
|
295
|
+
saveUser({
|
|
296
|
+
...mgmtUser,
|
|
297
|
+
uid: user.uid,
|
|
298
|
+
...(needsPhotoUpdate ? { photoURL: user.photoURL } : {})
|
|
299
|
+
}).then(() => {
|
|
300
|
+
console.debug("User updated in user management system", mgmtUser);
|
|
301
|
+
}).catch(e => {
|
|
302
|
+
console.error("Error updating user in user management system", e);
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
console.debug("User found in user management system", mgmtUser);
|
|
306
|
+
return true;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
throw Error("Could not find a user with the provided email in the user management system.");
|
|
310
|
+
}, [loading, users]);
|
|
311
|
+
|
|
312
|
+
const userRoles = authController.user ? defineRolesFor(authController.user) : undefined;
|
|
313
|
+
const isAdmin = (userRoles ?? []).some(r => r.id === "admin");
|
|
314
|
+
|
|
315
|
+
const userRoleIds = userRoles?.map(r => r.id);
|
|
316
|
+
useEffect(() => {
|
|
317
|
+
console.debug("Setting user roles", {
|
|
318
|
+
userRoles,
|
|
319
|
+
roles
|
|
320
|
+
});
|
|
321
|
+
authController.setUserRoles?.(userRoles ?? []);
|
|
322
|
+
}, [userRoleIds]);
|
|
323
|
+
|
|
324
|
+
const getUser = useCallback((uid: string): USER | null => {
|
|
325
|
+
if (!users) return null;
|
|
326
|
+
const user = users.find(u => u.uid === uid);
|
|
327
|
+
return user ?? null;
|
|
328
|
+
}, [users]);
|
|
329
|
+
|
|
330
|
+
return {
|
|
331
|
+
loading,
|
|
332
|
+
roles,
|
|
333
|
+
users,
|
|
334
|
+
saveUser,
|
|
335
|
+
saveRole,
|
|
336
|
+
rolesError,
|
|
337
|
+
deleteUser,
|
|
338
|
+
deleteRole,
|
|
339
|
+
usersError,
|
|
340
|
+
isAdmin,
|
|
341
|
+
allowDefaultRolesCreation: allowDefaultRolesCreation === undefined ? true : allowDefaultRolesCreation,
|
|
342
|
+
includeCollectionConfigPermissions: Boolean(includeCollectionConfigPermissions),
|
|
343
|
+
defineRolesFor,
|
|
344
|
+
authenticator,
|
|
345
|
+
...authController,
|
|
346
|
+
initialLoading: authController.initialLoading || loading,
|
|
347
|
+
userRoles: userRoles,
|
|
348
|
+
getUser,
|
|
349
|
+
user: authController.user ? {
|
|
350
|
+
...authController.user,
|
|
351
|
+
roles: userRoles
|
|
352
|
+
} : null
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
const entitiesToUsers = (docs: Entity<Omit<UserWithRoleIds, "id">>[]): (UserWithRoleIds)[] => {
|
|
357
|
+
return docs.map((doc) => {
|
|
358
|
+
const data = doc.values as any;
|
|
359
|
+
const newVar = {
|
|
360
|
+
uid: doc.id,
|
|
361
|
+
...data,
|
|
362
|
+
created_on: data?.created_on,
|
|
363
|
+
updated_on: data?.updated_on
|
|
364
|
+
};
|
|
365
|
+
return newVar as (UserWithRoleIds);
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
const entityToRoles = (entities: Entity<Omit<Role, "id">>[]): Role[] => {
|
|
370
|
+
return entities.map((doc) => ({
|
|
371
|
+
id: doc.id,
|
|
372
|
+
...doc.values
|
|
373
|
+
} as Role));
|
|
374
|
+
}
|