I recently put registering and logging with facebook on my website, which works, but i have one problem. When I change the name, photo, etc on my website profile and log out, as soon as I log back in, the changes are not updated. I think the right approach here is to check whether a user has already registered with the email address that matches their social email address and if they have AND their name/profile photo are set, then we don’t update their name/profile photo. I don't know how to manage that.
(function($, document, window) {
var hello = window.hello;
var makeApiRequest = window.makeApiRequest;
var SocialAuth;
'use strict';
if (typeof window.hello === 'undefined') {
return;
}
SocialAuth = {
init: function() {
hello.init({
facebook: '1684686755117990',
google: '617421937193-dbfsdngr0j1i3g10ecdum7ugjf92t6r6.apps.googleusercontent.com'
}, {
'redirect_uri': '',
scope: 'basic, email, friends'
});
},
login: function(provider, callback) {
var self = this;
if (this.isLoggedIn()) {
typeof callback === 'function' && callback(true);
return;
}
hello(provider)
.login()
.then(function() {
var authToken, data;
if (provider == 'facebook') {
authToken = JSON.parse(localStorage.hello).facebook['access_token'];
data = {
'token': authToken, 'provider': 'facebook'
};
}
else if (provider == 'google') {
authToken = JSON.parse(localStorage.hello).google['access_token'];
data = {
'token': authToken, 'provider': 'google'
};
}
makeApiRequest('auth/oauth', data, 'POST')
.success(function(response) {
self.setLoginToken(true);
self.setAccountType(provider);
typeof callback === 'function' && callback(true);
})
.error(function() {
typeof callback === 'function' && callback(false);
});
}, function() {
typeof callback === 'function' && callback(false);
});
},
logout: function(callback) {
hello.logout()
.then(callback);
},
isLoggedIn: function() {
return !!localStorage.ProjectToken;
},
getLoginToken: function() {
return localStorage.ProjectToken;
},
setLoginToken: function(token) {
localStorage.Project = token;
},
setAccountType: function(type) {
localStorage.accountType = type;
},
getUserData: function(provider) {
var data;
hello(provider)
.api('me')
.then(function(json) {
data = {};
data.email = json.email;
data['first_name'] = json['first_name'];
data['family_name'] = json['last_name'];
if (provider == 'facebook') {
data.avatar = json.picture + '?width=200&height=200';
}
else if (provider == 'google') {
data.avatar = json.picture.split('?')[0] + '?sz=200';
}
makeApiRequest('users/me', data, 'PATCH', true);
});
}
};
SocialAuth.init();
window.SocialAuth = SocialAuth;
}(jQuery, document, window));
What I have tried:
I don't know how to put it to work