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());
});
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With