1
votes

After registering with Firebase Authentication "Email / Password",saving e-mail without verification.I have application with Flutter firebase. When someone registers, I direct them to an email verification page and hold them there until they verify the email.The problem is that if someone uses my email and deletes app without verifying it, the mail still remains in the database.How do we delete unverified email addresses?

3
Hey Raghim. Did you make any progress on this? Two people tried to help you with answers below. If an answer was useful, click the upvote button (▲) to the left of it. If it answered your question, click the checkmark (✓) to accept it. That way others know that you've been (sufficiently) helped. Also see What should I do when someone answers my question?Frank van Puffelen
Hey Frank, Sorry for being late.There was a different urgent problem, so I cannot deal with this issue right now, but I will return to this issue as soon as possible.I thought Flutter firebase had a simple way to do this.I think it's easier and safer to sign in with "email link (passwordless sign-in)". I will return after trying it, considering your comments. @FrankvanPuffelenRaghim Najafov

3 Answers

2
votes

You can run a scheduled cloud function every day that checks for unverified users and deletes them. That also means you would have to use Admin SDK and cannot be done in Flutter. You can create a NodeJS Cloud Function with the following code and run it.

exports.scheduledFunction = functions.pubsub.schedule('every 24 hours').onRun((context) => {
    console.log('This will be run every 24 hours!');
    const users = []
    const listAllUsers = (nextPageToken) => {
        // List batch of users, 1000 at a time.
        return admin.auth().listUsers(1000, nextPageToken).then((listUsersResult) => {
            listUsersResult.users.forEach((userRecord) => {
                users.push(userRecord)
            });
            if (listUsersResult.pageToken) {
                // List next batch of users.
                listAllUsers(listUsersResult.pageToken);
            }
        }).catch((error) => {
            console.log('Error listing users:', error);
        });
    };
    // Start listing users from the beginning, 1000 at a time.
    await listAllUsers();
    const unVerifiedUsers = users.filter((user) => !user.emailVerified).map((user) => user.uid)

    //DELETING USERS
    return admin.auth().deleteUsers(unVerifiedUsers).then((deleteUsersResult) => {
        console.log(`Successfully deleted ${deleteUsersResult.successCount} users`);
        console.log(`Failed to delete ${deleteUsersResult.failureCount} users`);
        deleteUsersResult.errors.forEach((err) => {
            console.log(err.error.toJSON());
        });
        return true
    }).catch((error) => {
        console.log('Error deleting users:', error);
        return false
    });
});
0
votes

You can delete users through the Firebase Admin SDK.

You'll need a list of unverified users, either by listing all users and filtering it down, or from somewhere you store this yourself, and then you can delete the unverified users.

0
votes

This works just perfect:

const functions = require("firebase-functions");
const admin = require("firebase-admin");

admin.initializeApp();
exports.scheduledFunction = functions.pubsub
  .schedule("every 24 hours")
  .onRun((context) => {
    console.log("This will be run every 24 hours!");
    var users = [];
    var unVerifiedUsers = [];
    const listAllUsers = async (nextPageToken) => {
      // List batch of users, 1000 at a time.
      return admin
        .auth()
        .listUsers(1000, nextPageToken)
        .then((listUsersResult) => {
          listUsersResult.users.forEach((userRecord) => {
            users.push(userRecord);
          });
          if (listUsersResult.pageToken) {
            // List next batch of users.
            listAllUsers(listUsersResult.pageToken);
          }
        })
        .catch((error) => {
          console.log("Error listing users:", error);
        });
    };
    // Start listing users from the beginning, 1000 at a time.
    listAllUsers().then(() => {
      unVerifiedUsers = users
        .filter((user) => !user.emailVerified)
        .map((user) => user.uid);
      admin
        .auth()
        .deleteUsers(unVerifiedUsers)
        .then((deleteUsersResult) => {
          console.log(
            `Successfully deleted ${deleteUsersResult.successCount} users`
          );
          console.log(
            `Failed to delete ${deleteUsersResult.failureCount} users`
          );
          deleteUsersResult.errors.forEach((err) => {
            console.log(err.error.toJSON());
          });
          return true;
        })
        .catch((error) => {
          console.log("Error deleting users:", error);
          return false;
        });
    });
  });