Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor.js on login event

So I'm rather new to the meteor framework and JavaScript in general, but I'm working on a little project using the framework to try and bring myself up to scratch. Basically I'm working on a micro blogging site.

At the moment users are able to log in through several services, fb, google etc. And I retrieve their avatars via the service id inserted into the needed url, all this works fine. But I want the user to be able to see their own avatar as soon as they log in which means I need to run some JavaScript right after login is successful. At this point in time I cant find anything on an onLogin style event. And have hacked together a handlebars template to run the code when a user logs in. However this code appears to only run once and if a user logs out and then back in they don't see their avatar anymore.

Does anyone know of an event I can use to do this?

here is my hacky template

{{#if currentUser}}
    {{> userInput}}
{{/if}}

here is the js it calls

$('#inputAvatar').css('background-image', 'url('+avatarUrl+')');

I would really appreciate the help, I'm sure it's something simple that I've overlooked but I cant seem to figure it out!

Thanks, Peter.

like image 944
Peter McKinney Avatar asked May 03 '13 01:05

Peter McKinney


2 Answers

I don't know if the way you're doing it is the most appropriate but personally do the following on the client side to detect if a user logged in:

Tracker.autorun(function(){
  if(Meteor.userId()){
    //do your stuff
  }
});
like image 145
Martin Avatar answered Nov 03 '22 03:11

Martin


If you are using google login or any other o-auth login, you can pass a callback function which will gets execute after login,

Here is the sample code.

Meteor.loginWithGoogle({
  requestPermissions: ['email']
}, function(error) {
  if (error) {
    console.log(error); //If there is any error, will get error here
  }else{
    console.log(Meteor.user());// If there is successful login, you will get login details here
  }
});
like image 2
Laxmikant Dange Avatar answered Nov 03 '22 04:11

Laxmikant Dange