Yury Matatov
Yury Matatov

Reputation: 845

Cleanup user data from Cloud Firestore after sign out a user

I write a function witch cleanup all user data (collections, sub-collections, and images) from Cloud Firestore after he signs out from the account.

For this, I use the code from here.

My code:

'use strict';

const functions = require('firebase-functions');
const firebase_tools = require('firebase-tools');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
//const bucket = admin.storage().bucket("gs://movere-point.appspot.com/");

exports.recursiveDelete = functions.runWith({
    timeoutSeconds: 540,
    memory: '2GB'
})
.auth.user().onDelete((user) => {

    // const path = data.path;
    const path = "/Users/" + user.uid;

    console.log(path);

    return firebase_tools.firestore.delete(path, {
        project: process.env.GCLOUD_PROJECT,
        recursive: true,
        yes: true,
        token: ""
    })
    .then(() => {
        return {
            path: path 
        };
    });
});

I remove a user from the Firebase console, and in the log of Function, I get the error:

Error
    at new FirebaseError (/srv/node_modules/firebase-tools/lib/error.js:9:18)
    at module.exports (/srv/node_modules/firebase-tools/lib/responseToError.js:38:12)
    at Request._callback (/srv/node_modules/firebase-tools/lib/api.js:39:35)
    at Request.self.callback (/srv/node_modules/request/request.js:185:22)
    at emitTwo (events.js:126:13)
    at Request.emit (events.js:214:7)
    at Request.<anonymous> (/srv/node_modules/request/request.js:1161:10)
    at emitOne (events.js:116:13)
    at Request.emit (events.js:211:7)
    at IncomingMessage.<anonymous> (/srv/node_modules/request/request.js:1083:12)

Help solve the problem with the removal of all user data after the user leaves the program

Upvotes: 1

Views: 400

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83103

The link in your question is incorrect. However, I guess you refer to this documentation item: https://firebase.google.com/docs/firestore/solutions/delete-collections

You will note in this example the use of a token when doing:

return firebase_tools.firestore
  .delete(path, {
    project: process.env.GCLOUD_PROJECT,
    recursive: true,
    yes: true,
    token: functions.config().fb.token
  })

In your code you have assigned an empty value to this token, which is the reason of the error you encounter.

You need to do as follows, using the Firebase CLI in a terminal:

firebase login:ci

You will get a token value like:

✔  Success! Use this token to login on a CI server:

Z/A01c9zOI74FUgPJm5aEN9d2XyTKPgQkRlePQigxBBCSOUQ_0ktLW4mfAX3x4rFLL

Then you need to store it as environment configuration data. Since the token value is functions.config().fb.token you should do:

firebase functions:config:set fb.token="Z/A01c9zOI74FUgPJm5aEN9d2XyTKPgQkRlePQigxBBCSOUQ_0ktLW4mfAX3x4rFLL"

Then re-deploy the Cloud Functions and you should be ok!

Upvotes: 2

Related Questions