Accounts-password Meteor.logout() [SOLVED]

Hey guys, so i have been working on my meteor project for some time now and just now I have noticed a problem that I can’t figure out the solution to.

Using the accounts-password package (and its dependents) i made a custom user register/login. I tested it and everything was working fine, so the registration, logging in and logging out. I then moved on to the next steps in my project. Since meteor doesn’t really log you out when you end localhost, since finishing the custom login I have been developing under a testing account so i’m able to see the changes.

Today I decided to log out but when i tried to logging again nothing was happening. I checked the console and no error was popping up, so i entered Meteor.users.find().fetch(); which returned an empty array.

I looked at the log out code which is very straight forward:

"click .js-logout": function(event) {
        event.preventDefault();
        Meteor.logout();
        // Router.go('/');
},

but nothing seems wrong.

After looking at for a while i decided to create another account but when i logged out it deleted the account again. Then i went to the console and ran meteor update thinking it could be the problem, but no luck. I also tried restarting localhost but logging out would still delete the accounts i created. Then i though it was something to do with the package so i ran meteor remove accounts-password and added it again but it still didn’t fix the issue. I event tried installing an older version of the package but it still didn’t work.

I noticed there is a new version of npm-bcrypt but i cant install the latest (0.8.7) because when i run meteor add npm-bcrypt@0.8.7 it says:

=> Errors while adding packages:             
                                              
While selecting package versions:
error: Conflict: Constraint npm-bcrypt@=0.8.6_2 is not satisfied by npm-bcrypt 0.8.7.
Constraints on package "npm-bcrypt":
* npm-bcrypt@0.8.7 <- top level
* npm-bcrypt@=0.8.6_2 <- accounts-password 1.1.12

I tried to update accounts-password to the latest version (1.1.13) with meteor add accounts-password@1.1.13 but it also doesn’t work:

=> Errors while adding packages:             
                                              
While selecting package versions:
error: No version of accounts-password satisfies all constraints: @1.1.13, @=1.1.12
Constraints on package "accounts-password":
* accounts-password@1.1.13 <- top level
* accounts-password@=1.1.12 <- top level

I did some research but didn’t find anything.
Please help. Let me know if I should post a specific piece of code that would help.

Thank you in advance.

You say the account is deleted - did you check it’s still in MongoDB from the server side?

It’s not deleting the account. When you call Meteor.users.find().fetch(); on the client side you can only see what you have access to in the client side mini-mongo db. So since your user is logged out you don’t have access to the published Meteor.users collection. If you were to run the query Meteor.users.find().fetch(); in the mongo shell on the server side you would see that your user is still there.

To access the mongo shell open another terminal while your meteor app is running and type the command meteor mongo

1 Like

Hey, thank you for replying,

You are right i opened the mongo shell and ran db.users.find() and it returned all the accounts i created. So the reason why i cant log back in is because I didn’t publish the users collection correctly?

This is how i am publishing the users collection:

Meteor.publish("theUsers", function() {
  return Meteor.users.find({_id: this.userId}, {fields: {categories: 1, joined: 1, email: 1}});
});

How would i fix this?

I removed the condition “_id: this.userId” and it now returns the users in the console but every time i try to login now the console says user not found

Thank you so much.

Are you using the accounts-password package? It should just be a matter of calling Accounts.loginWithPassword - http://docs.meteor.com/api/accounts.html#Meteor-loginWithPassword

Or if you are using one of the accounts-ui packages it should just work. You don’t need to publish the users collection for the user to login (and it’s dangerous to do so because any visitor can access user data even if they are not logged in). Accounts will do the password checking on the server side and return a callback function which can use to see if the login was successful or not and then perform a task like redirecting them to another page.

Yeah, i’m using accounts-password, this is what i have:

Template.login.events({
    "submit .js-login": function() {
        event.preventDefault();

        const e = $(".js-email-login").val();
        const p = $(".js-password-login").val();

        Meteor.loginWithPassword(e, p, function(error) {
            if (error) {
                event.preventDefault();
                console.log(error.reason); // // console log reason [show display error to user]
                return;
            } else {
                Router.go("/upcoming");
            }
        });
    },
});

but it still says user not found in the console.

What could be the problem?

That looks correct. Are you sure the email is being sent it correctly?

Here’s the code that i’m using to register the user:

"submit .js-register": function(event) {
        event.preventDefault();

        const f = $(".js-first-name").val();
        const l = $(".js-last-name").val();
        const e = $(".js-email").val();
        const p = $(".js-password").val();

        Accounts.createUser({
          profile: {
            first: f,
            last: l
            // pic_url: ???
          },
          email: e,
          password: p
        }, function(error) {
          if(error) { // if registration fails
            console.log(error.reason); // console log reason
            return;
          } else {
            Router.go('/upcoming'); // else go to upcoming page
          }
        });
      },

And when I register i doesn’t give me any errors.

I also have code which runs upon user registration.

Accounts.onCreateUser(function(user) {

  // user.categories = [];


  const categories = [{
    name: "To-Do",
    tasks: [],
    goals: [],
    text: [],
    createdAt: new Date(),
    modified: new Date()
  }];

  // Meteor.call("insertDefault", defaultCategory);

  user.categories = categories;
  user.joined = new Date();

  return user;
});

in the Meteor.users collection this is how things are being stored:

One thing that i noticed is how email isn’t a object. it simply appears as a string.
Could that be the problem?
I used to show as a object but it doesn’t anymore.
I don’t know why it looks like that.

Yes that is definitely the problem. It should be an array like this.

“emails” : [
{
“address” : "myemail@emails.com",
“verified” : false
}
],

However your code for create user looks ok. Something else must be transforming or overwriting the email to a string.

So i commented out the Accounts.onCreateUser(function(user) {…

registered a new user and checked in the console and the email appears as a object again, then i logged out and was able to login again, greate!

But why would this:

Accounts.onCreateUser(function(user) {

  // user.categories = [];


  const categories = [{
    name: "To-Do",
    tasks: [],
    goals: [],
    text: [],
    createdAt: new Date(),
    modified: new Date()
  }];

  // Meteor.call("insertDefault", defaultCategory);

  user.categories = categories;
  user.joined = new Date();

  return user;
});

be causing that problem. doing anything to the email field only adding a categories array and a joined field.

What could be the problem?

1 Like

I figured it out, thank you so much!

The Accounts.onCreateUser() function was overwriting the collection fields, the documentation stated that it.

I had to send a options object with all the info i wanted as a parameter for the Accounts.onCreateUser() function and do the fields myself.

Thank you for brainstorming the issue with me.

I’m glad to hear it. I forgot as well that onCreateUser overwrites the default callback.

No problem.