Pup

v1.x

AccountsAccount Deletion

Pup includes the ability for users to delete their own accounts. This is provided mostly as an example for developers, giving you an action pattern that you can modify for your own use to add additional deletion layers for your users.

Inside of that action, we provide two methods deleteDocuments which deletes all of the users documents data and deleteUser which removes the user from the Meteor.users collection.

By default, the account deletion functionality is included in the /profile page of the app for all users. A "Delete My Account" button is provided on that page for both password-based and OAuth-based accounts.

/imports/api/Users/server/delete-account.js
/* eslint-disable consistent-return */

import { Meteor } from 'meteor/meteor';
import { Accounts } from 'meteor/accounts-base';
import Documents from '../../Documents/Documents';

let action;

const deleteUser = (userId) => {
  try {
    return Meteor.users.remove(userId);
  } catch (exception) {
    throw new Error(`[deleteAccount.deleteUser] ${exception.message}`);
  }
};

const deleteDocuments = (userId) => {
  try {
    return Documents.remove({ owner: userId });
  } catch (exception) {
    throw new Error(`[deleteAccount.deleteDocuments] ${exception.message}`);
  }
};

const deleteAccount = ({ userId }, promise) => {
  try {
    action = promise;
    deleteDocuments(userId);
    deleteUser(userId);
    action.resolve();
  } catch (exception) {
    action.reject(exception.message);
  }
};

export default options =>
  new Promise((resolve, reject) =>
    deleteAccount(options, { resolve, reject }));