@edgedev/firebase 1.8.12 → 1.9.1

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/README.md CHANGED
@@ -239,6 +239,63 @@ Calling this will generate a Microsoft Sign In Popup and register the user using
239
239
  );
240
240
  ```
241
241
 
242
+ ### Inviting an Existing User to Register with a New Organization or Member
243
+
244
+ To invite an existing user to register with a new organization or member's data and get the corresponding roles, use the `edgeFirebase.currentUserRegister(userRegister)` method.
245
+
246
+ ```javascript
247
+ const userRegisterData = {
248
+ registrationCode: "12345",
249
+ dynamicDocumentFieldValue: "fieldName",
250
+ };
251
+
252
+ const response = await edgeFirebase.currentUserRegister(userRegisterData);
253
+ ```
254
+
255
+ #### Parameters
256
+
257
+ - `userRegister` (object): An object containing the user registration data. It must include a `registrationCode` property provided by the inviting organization or member. It can also include a `dynamicDocumentFieldValue` property, which is a single string representing the name of an additional data field for registration.
258
+
259
+ ```typescript
260
+ interface userRegister {
261
+ registrationCode: string;
262
+ dynamicDocumentFieldValue?: string;
263
+ }
264
+ ```
265
+
266
+ #### Returns
267
+
268
+ The method returns a Promise that resolves to an `actionResponse` object:
269
+
270
+ ```typescript
271
+ interface actionResponse {
272
+ success: boolean;
273
+ message: string;
274
+ meta: {};
275
+ }
276
+ ```
277
+
278
+ Example usage:
279
+
280
+ ```javascript
281
+ <script setup>
282
+ async function inviteExistingUser() {
283
+ const userRegisterData = {
284
+ registrationCode: "12345",
285
+ dynamicDocumentFieldValue: "fieldName",
286
+ };
287
+
288
+ const response = await edgeFirebase.currentUserRegister(userRegisterData);
289
+ if (response.success) {
290
+ console.log("Existing user invited and registered successfully");
291
+ } else {
292
+ console.error("Error inviting and registering existing user:", response.message);
293
+ }
294
+ }
295
+ </script>
296
+ ```
297
+
298
+
242
299
  ### Explanation of permissions
243
300
 
244
301
  - **assign: boolean** - When a user has this permission for a collection they can assign other users to the collection and change permissions for that collection. For a user to be able run setUser, storeCollectionPermisions, storeUserRoles, removeUserRoles, storeUserSpecialPermissions, or removeUserSpecialPermissions, they must have assign access to any of the collection paths passed into those functions.
package/edgeFirebase.ts CHANGED
@@ -126,6 +126,7 @@ interface newUser {
126
126
  }
127
127
 
128
128
  interface userRegister {
129
+ uid?: string;
129
130
  email?: string;
130
131
  password?: string;
131
132
  meta: object;
@@ -233,7 +234,7 @@ export const EdgeFirebase = class {
233
234
 
234
235
  private functions = null;
235
236
 
236
- public runFunction = async (functionName: string, data: { [key: string]: unknown }) => {
237
+ public runFunction = async (functionName: string, data: Record<string, unknown>) => {
237
238
  data.uid = this.user.uid;
238
239
  const callable = httpsCallable(this.functions, functionName);
239
240
  return await callable(data);
@@ -462,6 +463,32 @@ export const EdgeFirebase = class {
462
463
  }
463
464
  }
464
465
 
466
+ public currentUserRegister = async (userRegister: userRegister): Promise<actionResponse> => {
467
+ if (!Object.prototype.hasOwnProperty.call(userRegister, 'registrationCode') || userRegister.registrationCode === "") {
468
+ return this.sendResponse({
469
+ success: false,
470
+ message: "Registration code is required.",
471
+ meta: {}
472
+ });
473
+ }
474
+ // userRegister.uid = this.user.uid;
475
+ const result = await this.runFunction("currentUserRegister", userRegister as unknown as Record<string, unknown>);
476
+ const resultData = result.data as {success: boolean, message: string};
477
+ if (resultData.success) {
478
+ return this.sendResponse({
479
+ success: true,
480
+ message: "",
481
+ meta: {}
482
+ });
483
+ } else {
484
+ return this.sendResponse({
485
+ success: false,
486
+ message: resultData.message,
487
+ meta: {}
488
+ });
489
+ }
490
+ }
491
+
465
492
  public registerUser = async (
466
493
  userRegister: userRegister,
467
494
  authProvider: authProviders = "email",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@edgedev/firebase",
3
- "version": "1.8.12",
3
+ "version": "1.9.1",
4
4
  "description": "Vue 3 / Nuxt 3 Plugin or Nuxt 3 plugin for firebase authentication and firestore.",
5
5
  "main": "index.ts",
6
6
  "scripts": {
package/src/functions.js CHANGED
@@ -34,6 +34,56 @@ exports.removeNonRegisteredUser = functions.https.onCall(async (data, context) =
34
34
  return { success: false, message: 'Non-registered user not found.' }
35
35
  })
36
36
 
37
+ exports.currentUserRegister = functions.https.onCall(async (data, context) => {
38
+ if (data.uid === context.auth.uid) {
39
+ const stagedUser = await db.collection('staged-users').doc(data.registrationCode).get()
40
+ if (!stagedUser.exists) {
41
+ return { success: false, message: 'Registration code not found.' }
42
+ }
43
+ else {
44
+ const stagedUserData = await stagedUser.data()
45
+ let process = false
46
+ if (stagedUserData.isTemplate) {
47
+ process = true
48
+ }
49
+ if (!stagedUserData.isTemplate && stagedUserData.userId === '') {
50
+ process = true
51
+ }
52
+ if (!process) {
53
+ return { success: false, message: 'Registration code not valid.' }
54
+ }
55
+ const newRoles = stagedUserData.roles || {}
56
+ const currentUser = await db.collection('users').doc(data.uid).get()
57
+ const currentUserData = await currentUser.data()
58
+ const currentRoles = currentUserData.roles || {}
59
+ const currentUserCollectionPaths = currentUserData.collectionPaths || []
60
+ let newRole = {}
61
+ if (stagedUserData.subCreate && Object.keys(stagedUserData.subCreate).length !== 0 && stagedUserData.isTemplate) {
62
+ if (!data.dynamicDocumentFieldValue) {
63
+ return { success: false, message: 'Dynamic document field value is required.' }
64
+ }
65
+ const rootPath = stagedUserData.subCreate.rootPath
66
+ const newDoc = stagedUserData.subCreate.documentStructure
67
+ newDoc[stagedUserData.subCreate.dynamicDocumentField] = data.dynamicDocumentFieldValue
68
+ const addedDoc = await db.collection(rootPath).add(newDoc)
69
+ await db.collection(rootPath).doc(addedDoc.id).update({ docId: addedDoc.id })
70
+ newRole = { [`${rootPath}-${addedDoc.id}`]: { collectionPath: `${rootPath}-${addedDoc.id}`, role: stagedUserData.subCreate.role } }
71
+ }
72
+ const combinedRoles = { ...currentRoles, ...newRoles, ...newRole }
73
+ Object.values(combinedRoles).forEach((role) => {
74
+ if (!currentUserCollectionPaths.includes(role.collectionPath)) {
75
+ currentUserCollectionPaths.push(role.collectionPath)
76
+ }
77
+ })
78
+ await db.collection('staged-users').doc(currentUserData.stagedDocId).update({ roles: combinedRoles, collectionPaths: currentUserCollectionPaths })
79
+ if (!stagedUserData.isTemplate) {
80
+ await db.collection('staged-users').doc(data.registrationCode).delete()
81
+ }
82
+ return { success: true, message: '' }
83
+ }
84
+ }
85
+ })
86
+
37
87
  exports.updateUser = functions.firestore.document('staged-users/{docId}').onUpdate((change, context) => {
38
88
  const eventId = context.eventId
39
89
  const eventRef = db.collection('events').doc(eventId)