@edgedev/firebase 1.4.6 → 1.4.8

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
@@ -170,6 +170,15 @@ edgeFirebase.storeCollectionPermissions(
170
170
  );
171
171
  ```
172
172
 
173
+ Deleting collection permissions. This is done to "clean up" whenever a collection path is being deleted.
174
+
175
+ ```javascript
176
+ edgeFirebase.removeCollectionPermissions(
177
+ "myItems/subitems/things")
178
+ ```
179
+
180
+
181
+
173
182
  ### User roles for collections
174
183
 
175
184
  Users are assigned roles based on collection paths. A role assigned by a collection path that has sub collections will also determine what the user can do on all sub collections or a user can be assigned a role specifically for a sub collection only. For example if a user is assigned as admin for "myItems/subitems/things" they will only have admin acces to that collection. But if the user is assigned as an admin for "myItems" they will have the admin permissions for "myItems" and all sub collections of "myItems".
package/edgeFirebase.ts CHANGED
@@ -148,6 +148,7 @@ interface firebaseConfig {
148
148
  interface actionResponse {
149
149
  success: boolean;
150
150
  message: string;
151
+ meta: object;
151
152
  }
152
153
 
153
154
  interface permissionStatus {
@@ -210,7 +211,7 @@ export const EdgeFirebase = class {
210
211
  }
211
212
  }
212
213
  this.user.specialPermissions = specialPermissions;
213
- await this.listCollectionsCanAssign()
214
+ this.listCollectionsCanAssign()
214
215
  }
215
216
  const metaUnsubscribe = onSnapshot(
216
217
  doc(this.db, "users", this.user.email),
@@ -266,7 +267,6 @@ export const EdgeFirebase = class {
266
267
  this.user.uid = userAuth.uid;
267
268
  this.user.logInError = false;
268
269
  this.user.logInErrorMessage = "";
269
-
270
270
  this.startUserMetaSync();
271
271
  } else {
272
272
  this.user.email = "";
@@ -288,7 +288,8 @@ export const EdgeFirebase = class {
288
288
  if (user.userId) {
289
289
  return this.sendResponse({
290
290
  success: false,
291
- message: "User already registered"
291
+ message: "User already registered",
292
+ meta: {}
292
293
  });
293
294
  } else {
294
295
  createUserWithEmailAndPassword(
@@ -305,14 +306,16 @@ export const EdgeFirebase = class {
305
306
  }
306
307
  return this.sendResponse({
307
308
  success: true,
308
- message: ""
309
+ message: "",
310
+ meta: {}
309
311
  });
310
312
  });
311
313
  }
312
314
  } else {
313
315
  return this.sendResponse({
314
316
  success: false,
315
- message: "User doesn't exist"
317
+ message: "User doesn't exist",
318
+ meta: {}
316
319
  });
317
320
  }
318
321
  };
@@ -322,12 +325,14 @@ export const EdgeFirebase = class {
322
325
  await sendPasswordResetEmail(this.auth, email);
323
326
  return this.sendResponse({
324
327
  success: true,
325
- message: ""
328
+ message: "",
329
+ meta: {}
326
330
  });
327
331
  } catch (error) {
328
332
  return this.sendResponse({
329
333
  success: false,
330
- message: error.message
334
+ message: error.message,
335
+ meta: {}
331
336
  });
332
337
  }
333
338
  };
@@ -341,12 +346,14 @@ export const EdgeFirebase = class {
341
346
  await confirmPasswordReset(this.auth, oobCode, password);
342
347
  return this.sendResponse({
343
348
  success: true,
344
- message: ""
349
+ message: "",
350
+ meta: {}
345
351
  });
346
352
  } catch (error) {
347
353
  return this.sendResponse({
348
354
  success: false,
349
- message: error.message
355
+ message: error.message,
356
+ meta: {}
350
357
  });
351
358
  }
352
359
  };
@@ -365,12 +372,14 @@ export const EdgeFirebase = class {
365
372
  await updatePassword(user, password);
366
373
  return this.sendResponse({
367
374
  success: true,
368
- message: ""
375
+ message: "",
376
+ meta: {}
369
377
  });
370
378
  } catch (error) {
371
379
  return this.sendResponse({
372
380
  success: false,
373
- message: error.message
381
+ message: error.message,
382
+ meta: {}
374
383
  });
375
384
  }
376
385
  };
@@ -383,7 +392,8 @@ export const EdgeFirebase = class {
383
392
  }
384
393
  return this.sendResponse({
385
394
  success: true,
386
- message: ""
395
+ message: "",
396
+ meta: {}
387
397
  });
388
398
  };
389
399
 
@@ -421,12 +431,14 @@ export const EdgeFirebase = class {
421
431
  if (removedFrom.length > 0) {
422
432
  return this.sendResponse({
423
433
  success: true,
424
- message: ""
434
+ message: "",
435
+ meta: {}
425
436
  });
426
437
  } else {
427
438
  return this.sendResponse({
428
439
  success: false,
429
- message: "You do not have permission to remove this user"
440
+ message: "You do not have permission to remove this user",
441
+ meta: {}
430
442
  });
431
443
  }
432
444
  };
@@ -455,12 +467,14 @@ export const EdgeFirebase = class {
455
467
  this.generateUserMeta(userMeta);
456
468
  return this.sendResponse({
457
469
  success: true,
458
- message: ""
470
+ message: "",
471
+ meta: {}
459
472
  });
460
473
  } else {
461
474
  return this.sendResponse({
462
475
  success: false,
463
- message: "User already exists"
476
+ message: "User already exists",
477
+ meta: {}
464
478
  });
465
479
  }
466
480
  } else {
@@ -470,7 +484,8 @@ export const EdgeFirebase = class {
470
484
  "Cannot assign role or special permission for collection path(s): " +
471
485
  canAssignRole.badCollectionPaths
472
486
  .concat(canAssignSpecialPermissions.badCollectionPaths)
473
- .join(", ")
487
+ .join(", "),
488
+ meta: {}
474
489
  });
475
490
  }
476
491
  };
@@ -916,12 +931,14 @@ export const EdgeFirebase = class {
916
931
  }
917
932
  return sendResponse({
918
933
  success: true,
919
- message: ""
934
+ message: "",
935
+ meta: {}
920
936
  });
921
937
  } else {
922
938
  return sendResponse({
923
939
  success: false,
924
- message: `You do not have permission to read from "${collectionPath}"`
940
+ message: `You do not have permission to read from "${collectionPath}"`,
941
+ meta: {}
925
942
  });
926
943
  }
927
944
  };
@@ -991,12 +1008,14 @@ export const EdgeFirebase = class {
991
1008
  this.unsubscibe[collectionPath] = unsubscribe;
992
1009
  return this.sendResponse({
993
1010
  success: true,
994
- message: ""
1011
+ message: "",
1012
+ meta: {}
995
1013
  });
996
1014
  } else {
997
1015
  return this.sendResponse({
998
1016
  success: false,
999
- message: `You do not have permission to read from "${collectionPath}"`
1017
+ message: `You do not have permission to read from "${collectionPath}"`,
1018
+ meta: {}
1000
1019
  });
1001
1020
  }
1002
1021
  };
@@ -1125,13 +1144,15 @@ export const EdgeFirebase = class {
1125
1144
  });
1126
1145
  return this.sendResponse({
1127
1146
  success: true,
1128
- message: ""
1147
+ message: "",
1148
+ meta: {}
1129
1149
  });
1130
1150
  } else {
1131
1151
  return this.sendResponse({
1132
1152
  success: false,
1133
1153
  message:
1134
- "Cannot remove permissions for collection path: " + collectionPath
1154
+ "Cannot remove permissions for collection path: " + collectionPath,
1155
+ meta: {}
1135
1156
  });
1136
1157
  }
1137
1158
  };
@@ -1148,13 +1169,15 @@ export const EdgeFirebase = class {
1148
1169
  });
1149
1170
  return this.sendResponse({
1150
1171
  success: true,
1151
- message: ""
1172
+ message: "",
1173
+ meta: {}
1152
1174
  });
1153
1175
  } else {
1154
1176
  return this.sendResponse({
1155
1177
  success: false,
1156
1178
  message:
1157
- "Cannot remove permissions for collection path: " + collectionPath
1179
+ "Cannot remove permissions for collection path: " + collectionPath,
1180
+ meta: {}
1158
1181
  });
1159
1182
  }
1160
1183
  };
@@ -1177,19 +1200,22 @@ export const EdgeFirebase = class {
1177
1200
  updateDoc(doc(this.db, "users/" + email), permissionItem);
1178
1201
  return this.sendResponse({
1179
1202
  success: true,
1180
- message: ""
1203
+ message: "",
1204
+ meta: {}
1181
1205
  });
1182
1206
  } else {
1183
1207
  return this.sendResponse({
1184
1208
  success: false,
1185
- message: collectionPath + " is not a valid collection path"
1209
+ message: collectionPath + " is not a valid collection path",
1210
+ meta: {}
1186
1211
  });
1187
1212
  }
1188
1213
  } else {
1189
1214
  return this.sendResponse({
1190
1215
  success: false,
1191
1216
  message:
1192
- "Cannot assign permissions for collection path: " + collectionPath
1217
+ "Cannot assign permissions for collection path: " + collectionPath,
1218
+ meta: {}
1193
1219
  });
1194
1220
  }
1195
1221
  };
@@ -1215,25 +1241,49 @@ export const EdgeFirebase = class {
1215
1241
  updateDoc(doc(this.db, "users/" + email), roleItem);
1216
1242
  return this.sendResponse({
1217
1243
  success: true,
1218
- message: ""
1244
+ message: "",
1245
+ meta: {}
1219
1246
  });
1220
1247
  } else {
1221
1248
  return this.sendResponse({
1222
1249
  success: false,
1223
- message: collectionPath + " is not a valid collection path"
1250
+ message: collectionPath + " is not a valid collection path",
1251
+ meta: {}
1224
1252
  });
1225
1253
  }
1226
1254
  } else {
1227
1255
  return this.sendResponse({
1228
1256
  success: false,
1229
- message: "Role must be either 'admin' or 'user'"
1257
+ message: "Role must be either 'admin' or 'user'",
1258
+ meta: {}
1230
1259
  });
1231
1260
  }
1232
1261
  } else {
1233
1262
  return this.sendResponse({
1234
1263
  success: false,
1235
1264
  message:
1236
- "Cannot assign permissions for collection path: " + collectionPath
1265
+ "Cannot assign permissions for collection path: " + collectionPath,
1266
+ meta: {}
1267
+ });
1268
+ }
1269
+ };
1270
+
1271
+ public removeCollectionPermissions = async (
1272
+ collectionPath: string,
1273
+ ): Promise<actionResponse> => {
1274
+ const canAssign = await this.permissionCheck("assign", collectionPath);
1275
+ if (canAssign) {
1276
+ await deleteDoc(doc(this.db, "collection-data", collectionPath.replaceAll("/", "-")));
1277
+ return this.sendResponse({
1278
+ success: true,
1279
+ message: "",
1280
+ meta: {}
1281
+ });
1282
+ } else {
1283
+ return this.sendResponse({
1284
+ success: false,
1285
+ message: "Cannot remove permissions for collection path: " + collectionPath,
1286
+ meta: {}
1237
1287
  });
1238
1288
  }
1239
1289
  };
@@ -1244,7 +1294,7 @@ export const EdgeFirebase = class {
1244
1294
  permissions: permissions
1245
1295
  ): Promise<actionResponse> => {
1246
1296
  const canAssign = await this.permissionCheck("assign", collectionPath);
1247
-
1297
+ // TODO: check if collectionPath starts with "users" and deny if so
1248
1298
  if (canAssign) {
1249
1299
  if (role === "admin" || role === "user") {
1250
1300
  const currentTime = new Date().getTime();
@@ -1272,19 +1322,22 @@ export const EdgeFirebase = class {
1272
1322
 
1273
1323
  return this.sendResponse({
1274
1324
  success: true,
1275
- message: ""
1325
+ message: "",
1326
+ meta: {}
1276
1327
  });
1277
1328
  } else {
1278
1329
  return this.sendResponse({
1279
1330
  success: false,
1280
- message: "Role must be either 'admin' or 'user'"
1331
+ message: "Role must be either 'admin' or 'user'",
1332
+ meta: {}
1281
1333
  });
1282
1334
  }
1283
1335
  } else {
1284
1336
  return this.sendResponse({
1285
1337
  success: false,
1286
1338
  message:
1287
- "Cannot assign permissions for collection path: " + collectionPath
1339
+ "Cannot assign permissions for collection path: " + collectionPath,
1340
+ meta: {}
1288
1341
  });
1289
1342
  }
1290
1343
  };
@@ -1299,7 +1352,8 @@ export const EdgeFirebase = class {
1299
1352
  if (!canWrite) {
1300
1353
  return this.sendResponse({
1301
1354
  success: false,
1302
- message: `You do not have permission to write to "${collectionPath}"`
1355
+ message: `You do not have permission to write to "${collectionPath}"`,
1356
+ meta: {}
1303
1357
  });
1304
1358
  } else {
1305
1359
  if (generatePermissions) {
@@ -1322,6 +1376,11 @@ export const EdgeFirebase = class {
1322
1376
  }
1323
1377
  }
1324
1378
  setDoc(doc(this.db, collectionPath, docId), cloneItem);
1379
+ return this.sendResponse({
1380
+ success: true,
1381
+ message: "",
1382
+ meta: {docId}
1383
+ });
1325
1384
  } else {
1326
1385
  const docRef = await addDoc(
1327
1386
  collection(this.db, collectionPath),
@@ -1338,11 +1397,12 @@ export const EdgeFirebase = class {
1338
1397
  { ...cloneItem, docId: docRef.id },
1339
1398
  generatePermissions
1340
1399
  );
1400
+ return this.sendResponse({
1401
+ success: true,
1402
+ message: "",
1403
+ meta: {docId: docRef.id}
1404
+ });
1341
1405
  }
1342
- return this.sendResponse({
1343
- success: true,
1344
- message: ""
1345
- });
1346
1406
  }
1347
1407
  };
1348
1408
 
@@ -1363,12 +1423,14 @@ export const EdgeFirebase = class {
1363
1423
  deleteDoc(doc(this.db, collectionPath, docId));
1364
1424
  return this.sendResponse({
1365
1425
  success: true,
1366
- message: ""
1426
+ message: "",
1427
+ meta: {}
1367
1428
  });
1368
1429
  } else {
1369
1430
  return this.sendResponse({
1370
1431
  success: false,
1371
- message: `You do not have permission to delete from "${collectionPath}"`
1432
+ message: `You do not have permission to delete from "${collectionPath}"`,
1433
+ meta: {}
1372
1434
  });
1373
1435
  }
1374
1436
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@edgedev/firebase",
3
- "version": "1.4.6",
3
+ "version": "1.4.8",
4
4
  "description": "Vue 3 / Nuxt 3 Plugin or Nuxt 3 global composable for firebase authentication and firestore.",
5
5
  "main": "index.ts",
6
6
  "scripts": {
@@ -30,6 +30,7 @@
30
30
  "firebase": "^9.12.1",
31
31
  "prettier": "^2.7.1",
32
32
  "typescript": "^4.8.4",
33
+ "vite": "^4.0.4",
33
34
  "vue": "^3.2.41"
34
35
  },
35
36
  "peerDependencies": {