-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathindex.js
74 lines (64 loc) · 2.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const { initializeApp } = require('firebase-admin/app');
const { getAuth } = require('firebase-admin/auth');
const firebase_tools = require('firebase-tools');
const functions = require('firebase-functions');
initializeApp();
/**
* Callable function that creates a custom auth token with the
* custom attribute "admin" set to true.
*
* See https://firebase.google.com/docs/auth/admin/create-custom-tokens
* for more information on creating custom tokens.
*
* @param {string} data.uid the user UID to set on the token.
*/
exports.mintAdminToken = functions.https.onCall(async (data, context) => {
const uid = data.uid;
const token = await getAuth()
.createCustomToken(uid, { admin: true });
return { token };
});
// [START recursive_delete_function]
/**
* Initiate a recursive delete of documents at a given path.
*
* The calling user must be authenticated and have the custom "admin" attribute
* set to true on the auth token.
*
* This delete is NOT an atomic operation and it's possible
* that it may fail after only deleting some documents.
*
* @param {string} data.path the document or collection path to delete.
*/
exports.recursiveDelete = functions
.runWith({
timeoutSeconds: 540,
memory: '2GB'
})
.https.onCall(async (data, context) => {
// Only allow admin users to execute this function.
if (!(context.auth && context.auth.token && context.auth.token.admin)) {
throw new functions.https.HttpsError(
'permission-denied',
'Must be an administrative user to initiate delete.'
);
}
const path = data.path;
console.log(
`User ${context.auth.uid} has requested to delete path ${path}`
);
// Run a recursive delete on the given document or collection path.
// The 'token' must be set in the functions config, and can be generated
// at the command line by running 'firebase login:ci'.
await firebase_tools.firestore
.delete(path, {
project: process.env.GCLOUD_PROJECT,
recursive: true,
force: true,
token: functions.config().fb.token
});
return {
path: path
};
});
// [END recursive_delete_function]