Creating a user without a password in Meteor

I have a unique user creation stream that looks like this:

  • A user first appears on my site and clicks a button.
  • I create a User for them in the database and set the localStorage key with the UID.
  • Usage is about creating data, and I save the data in the database and associate it with the UID.
  • The user is returned, and if they have a UID set in localStorage, I show them the previously created data.
  • The user can click "Register" to create a "real" account with which they will have to log in with a username and password or another service (for example, Facebook).

So, how could I accomplish this with Meteor accounts and the User model?

In a nutshell:

  • I need to create a mongo user document without information (about the user).
  • I need to authenticate the user simply by having a UID (acting as a "password").
+7
source share
2 answers
  • Register onCreateUser to add an “anonymous” field ( {anonymous:1} ) when a random password is used, can be generated using Meteor.uuid() .
  • Add a timestamp field ( {created:new Date()} ) to clear old anonymous accounts.
  • Performing old anonymous user services, such as deleting anonymous users. than one hour: Meteor.autorun(function() {Meteor.users.find({anonymous:1,$where:"new Date() - this.created > 360000"}).forEach(function (user) {
    Meteor.users.remove({_id:user._id})}});
    Meteor.autorun(function() {Meteor.users.find({anonymous:1,$where:"new Date() - this.created > 360000"}).forEach(function (user) {
    Meteor.users.remove({_id:user._id})}});
  • On the client:
    • Always ask for a "nickname." This will become the official username or will be sitting in the system forever.
    • Check if the client is registered. If you do not create a user with an alias and password "magic number" that registers you. When they press case, write “Register” at the top, but actually just change your password and $set:{anonymous:0}

Do not use localStorage and do not use UID. Session cookie is your UID.

+8
source

I don’t know how to help with authentication, but as for creating an empty User object, I have successfully done the following on the server side (with a different name ...):

Meteor.users.insert({profile: {name: 'Oompa Loompa'}, foo: 'bar'});

+1
source

All Articles