This screencast shows how to retrieve additional user profile attributes from external authentication. But I don't understand how can I update the user account every time user logs in with possibly updated profile attributes? Is onCreateUser
called every time user authenticates or just the first time? From what I understand it is just the first time. So how can I hook into login process to update attributes?

- 6,756
- 5
- 54
- 86
-
Correct me if I am wrong: don't all users related operations get written to 'users' collection which you can observe? – imslavko Jul 29 '13 at 07:34
-
1@imslavko : the question is how to update user profile when his profile @ Facebook / Twitter / etc. changes. – Hubert OG Jul 29 '13 at 08:23
-
have you tried ripping out the Meteor.http.get call from onCreateUser and adding it to a helper that runs after the user has logged in? – booyaa Jul 29 '13 at 09:46
1 Answers
You need to hook into when someone logs in and then update the attributes manually.
Firstly you need something that tells when the user is logged in. At the moment you could use a client based solution (where a call is made to the server a second time on a successful login) using something like meteor-prescence or by editing the core packages and placing them in your /packages
directory.
Alter the accounts-base
package with the file accounts-server.js
to create a 'hook' when the user logs in at
Meteor.methods({
login: function(options) {
.....
if (result !== null)
//Run here
this.setUserId(result.id);
return result;
},
});
Then at the //Run Here
add a function that connects to facebook and gets the data you need. Or in the Meteor.call method that you would call from the client if you decide to use meteor-prescence
or a similar library or method. It would be something similar to this:
if(Meteor.user().services.facebook.accessToken) {
var graph = Npm.require('fbgraph');
graph.setAccessToken(Meteor.user().services.facebook.accessToken);
graph.get('/me',{},function(err,result) {
//Update your user (you could also alter services.facebook instead
Meteor.users.update(Meteor.userId, {$set: {profile:result}});
}
}
In the above the example is for facebook using the fbgraph
node module (which you would need to install) - or use the method described here to use Npm modules without a seperate package. You could to the same in principle for other providers too. Note you don't have to use a synchronous type call here to your provider as it is ok the data could be updated shortly after they log in.
Another place you could hook into is also in the _setUserId
method in the livedata
package.

- 75,157
- 39
- 215
- 276