I have the following situation:
The service I have is half functioning. I did not do it, so I want to modify it a bit so that I do two steps in one, that is, currently when I click on the button that I have for Facebook it only registers but. he does not login I've never touched the facebook api so I do not know where to start ...
I have the following: I use a javascript file that contains the functions that the action of the button occupies.
function FacebookAuthService(config) {
var self = this;
this.logged = false;
this.init = function () {
FB.init({
appId: config.fbappId,
cookie: true,
status: true,
xfbml: config.fbxfbml,
version: config.fbversion
});
FB.Event.subscribe('auth.authResponseChange', self.userStatusChange);
};
function fbLogin(perms, callback) {
perms = perms || '';
FB.login(function (response) {
if (response.status === 'connected') {
return callback({logged: true, uid: FB.getUserID()});
}
return callback({logged: false});
}, {scope: perms});
}
this.userStatusChange = function (response) {
self.logged = (response.status === 'connected');
};
this.login = function (perms, callback) {
//if (!self.logged) {
fbLogin(perms, callback);
//} else {
// callback({logged: true, uid: FB.getUserID()});
//}
};
this.getToken = function () {
return FB.getAccessToken();
};
}
/**
* Service for Facebook Graph API
*
* @constructor
*/
function FacebookGraphService() {
/**
* Calls to graph API
*
* @param path
* @param params
* @param callback
*/
this.graph = function (path, params, callback) {
params = params || {};
FB.api(path, params, function (response) {
var status = (response && !response.error);
callback(status, response);
});
};
}
/**
* Service for facebook user
*
* @constructor
*/
function FacebookUserService() {
/**
* @type {FacebookGraphService}
*/
var graphService = new FacebookGraphService();
/**
* Returns user's info
*
* @param uid
* @param callback
*/
this.getUserInfo = function (uid, callback) {
uid = uid || 'me';
graphService.graph('/' + uid, {fields: 'id,first_name,last_name,email'}, callback);
};
}
Inside the page where I load the content I have a script that generates the facebook sdk and initialize the main function
<script type="text/javascript">
// Load the SDK asynchronously
(function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s);
js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
var fbAuthService = {};
window.fbAsyncInit = function () {
fbAuthService = new FacebookAuthService(globalServerData);
fbAuthService.init();
};
</script>
And finally I have the action of the facebook button
$(document).on("click", ".js-login-facebook", function () {
$(".js-login-normal").attr("disabled", true);
var l = Ladda.create(document.querySelector(".js-login-facebook"));
l.start();
fbAuthService.login('email,public_profile', function (response) {
if (response.logged) {
var userService = new FacebookUserService();
userService.getUserInfo(response.uid, registerFbUser);
l.stop();
}
});
});
function registerFbUser(status, data) {
if (status) {
$.post(globalServerData.ajaxfbloginUrl, data, function (response) {
bootbox.hideAll();
console.log(response);
if (response.status == true) {
return location.reload(true);
}
bootbox.alert(response.message);
}, 'json');
}
}
The situation is that I do not know where to start ... It is supposed that when one clicks the button the record is made in the data DB, followed by this the page is reloaded but it does not start automatically ... Then I have to click the button again to be able to login ... That's the process I follow ...
Finally on the server side I have this that is part of facebook
public function actionFacebookLogin() {
/** @var Customer $customer */
try {
$guest = $this->getGuestActual();
if ($guest->isCustomer()) {
throw new ValidationException('Ya tienes una session iniciada.');
}
$service = new FacebookUserService();
$userInfo = $service->getUserInfo();
$customer = Customer::find()->where(['fb_uid' => $userInfo->id])->one();
if (empty($customer)) {
$customerService = new CustomerService();
$customer = $customerService->createByFacebook($userInfo);
}
$this->startCustomerSession($customer);
} catch (ValidationException $e) {
return json_encode(['status' => false, 'message' => $e->getMessage()]);
} catch (\Exception $e) {
return json_encode(['status' => false, 'message' => 'El usuario no existe.']);
}
return json_encode(['status' => true, 'message' => '']);
}