Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Account onLogin hook Meteor loop

I am building an application using Meteor. I want to create a new Cart ID (to act as a cart where I can store items) each time a user logs into my application. However, every time I open a new page in the application, a new Cart ID is created. Does this mean that the application "logs in" every single time I click on a new page in the app? Here's my code:

    Accounts.onLogin(function(user){
            var newCartId = uuid.new()
            Meteor.users.update({_id: user.user._id}, {$set: {'profile.cartId': newCartId}})
            console.log('just created a new Cart ID at ' + Date());
    });
like image 355
Trung Tran Avatar asked Jun 04 '15 13:06

Trung Tran


1 Answers

Yes, this is true.

Every time you open a new page you are not logged in. When the localStorage token authenticates you, similar to how a cookie does, you are logged in automatically. This hook will also run when you are logged in automatically.

Its difficult to define how a user logs in. Meteor's onLogin hook fires on any type of login Method.

You can customise when you want your hook to run, though:

Accounts.onLogin(function(info) {

    if(info.methodName == "createUser") {

        console.log("This user logged in by signing up");


    }else if(info.type == "password") {

        console.log("This user logged in by using his/her password");


    }else if(info.type == "resume") {

        console.log("This user logged in using a localStorage token");
    }
});

So here you can make the event fire only when a user logs in using his or her password. Or even when they sign up. You can use this to exclude running your hook if the user opens a new page, which uses the localStorage token to sign up.

like image 70
Tarang Avatar answered Sep 20 '22 08:09

Tarang