-
-
Save jazzbpn/96cc8b2d3cdc6ad93486934ebf1183a7 to your computer and use it in GitHub Desktop.
Coinverse Open App - Cloud Function: Delete User
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers. | |
const functions = require('firebase-functions'); | |
const firebase_tools = require('firebase-tools'); | |
// The Firebase Admin SDK to access the Firebase Realtime Database. | |
const admin = require('firebase-admin'); | |
admin.initializeApp(); | |
/** | |
* 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.deleteUser = functions | |
.runWith({ | |
timeoutSeconds: 540, | |
memory: '2GB' | |
}) | |
.https.onCall((data, context) => { | |
// Only allow admin users to execute this function. | |
console.log(`User auth: ${context.auth.uid} Data auth: ${data.userId}`); | |
if (context.auth.uid !== data.userId) { | |
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'. | |
return firebase_tools.firestore | |
.delete(path, { | |
project: process.env.GCLOUD_PROJECT, | |
recursive: true, | |
yes: true, | |
token: functions.config().fb.token | |
}) | |
.then(() => { | |
return { | |
path: path | |
}; | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment