How to reset a currently logged in user


#1

Hi,

I will like to assign a new userId to a currently logged in user so that the user data will be “reset” while still retaining references to the old userId.

Do I need to create a new accounts package to log this user off, and recreate a new user and log him on again?

It will be great if someone can point me to the right direction. Still trying to understand the accounts packages.

Thanks.


#2

Can you explain what user data you want to be “reset”? Do you mean you want to reset certain profile and/or custom user data fields, or do you mean you want to reset everything? If it’s everything, well then you’d be effectively destroying that user. So yes your approach would then be something like:

  1. Grab a copy of their current user ID via Meteor.userId().
  2. Remove the user: Meteor.users.remove
  3. Create the user: Accounts.createUser, with the desired new user details (this could include a reference to the old user ID if you want).
  4. Log the new user in: Meteor.loginWithPassword

That being said, you’d probably be better off keeping the initial user account, and just resetting the specific parts you want to change. This will make your life easier if the user’s old ID is linked to your data anywhere. You can change/update just about all parts of a user record through the Accounts API, while preserving the user’s account.