- Updated CSS for social login buttons to improve layout and hover effects. - Changed the text for the Facebook login button from "Sign in with Facebook" to "Zaloguj się za pomocą Facebook" for localization. - Adjusted JavaScript to reflect the new button text.
158 lines
4.9 KiB
JavaScript
158 lines
4.9 KiB
JavaScript
/**
|
|
* NOTICE OF LICENSE
|
|
*
|
|
* This source file is subject to the Software License Agreement
|
|
* that is bundled with this package in the file LICENSE.txt.
|
|
*
|
|
* @author Peter Sliacky (Zelarg)
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
|
|
*/
|
|
|
|
document.addEventListener('DOMContentLoaded', function(event) {
|
|
$('#login-form').before('<div id="tc-social-logins">' +
|
|
'<div id="tc-facebook-signin" class="tc-social-login-btn"><span class="social-logo"><span class="facebook-logo"></span></span>Zaloguj się za pomocą Facebook</div>' +
|
|
'<div id="tc-google-signin"></div>' +
|
|
'</div>')
|
|
});
|
|
|
|
|
|
// ========= FACEBOOK =========
|
|
var tc_facebookLogin = (function () {
|
|
|
|
var customBtnId = 'tc-facebook-signin';
|
|
|
|
function init() {
|
|
return attachCustomBtn();
|
|
}
|
|
|
|
function attachCustomBtn() {
|
|
document.getElementById(customBtnId).addEventListener('click', function () {
|
|
FB.login(statusChangeCallback, {scope: 'email,public_profile', return_scopes: true});
|
|
}, false);
|
|
document.getElementById(customBtnId).classList.add('enabled');
|
|
}
|
|
|
|
function backendSignIn(access_token) {
|
|
$.ajax({
|
|
url: prestashop.urls.pages.order,
|
|
type: 'POST',
|
|
cache: false,
|
|
dataType: "json",
|
|
data: "&ajax_request=1&action=socialLoginFacebook" +
|
|
"&access_token=" + access_token +
|
|
"&static_token=" + prestashop.static_token,
|
|
success: function (jsonData) {
|
|
if (jsonData.hasErrors) {
|
|
// TODO: better error handling
|
|
console.error(jsonData.errors);
|
|
} else if ('undefined' !== typeof jsonData.email && jsonData.email) {
|
|
location.reload();
|
|
}
|
|
},
|
|
error: function(jqXHR, textStatus, errorThrown) {
|
|
if(jqXHR.status === 500) {
|
|
console.error("Internal server error occurred: ", errorThrown);
|
|
// Add any additional error handling logic here
|
|
}
|
|
location.reload();
|
|
}
|
|
});
|
|
}
|
|
|
|
function statusChangeCallback(response) {
|
|
// The response object is returned with a status field that lets the app know the current login status of the person.
|
|
if (response.status === 'connected') {
|
|
backendSignIn(response.authResponse.accessToken);
|
|
} else if (response.status === 'not_authorized') {
|
|
// The person is logged into Facebook, but not your app.
|
|
} else {
|
|
// The person is not logged into Facebook, so we're not sure if they are logged into this app or not.
|
|
}
|
|
}
|
|
|
|
return {
|
|
init: init,
|
|
};
|
|
}());
|
|
|
|
|
|
// ========= GOOGLE =========
|
|
var tc_googleLogin = (function () {
|
|
|
|
var customBtnId = 'tc-google-signin';
|
|
|
|
function decodeJwtResponse (token) {
|
|
var base64Url = token.split('.')[1];
|
|
var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
|
|
var jsonPayload = decodeURIComponent(atob(base64).split('').map(function(c) {
|
|
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
|
|
}).join(''));
|
|
|
|
return JSON.parse(jsonPayload);
|
|
}
|
|
|
|
function handleCredentialResponse(response) {
|
|
// decodeJwtResponse() is a custom function defined by you
|
|
// to decode the credential response.
|
|
const responsePayload = decodeJwtResponse(response.credential);
|
|
var givenName = ' ';
|
|
var familyName = ' ';
|
|
if ('undefined' !== typeof responsePayload.given_name) {
|
|
givenName = responsePayload.given_name;
|
|
}
|
|
if ('undefined' !== typeof responsePayload.family_name) {
|
|
familyName = responsePayload.family_name;
|
|
}
|
|
backendSignIn(response.credential, givenName, familyName);
|
|
}
|
|
|
|
function init(google_client_id) {
|
|
google.accounts.id.initialize({
|
|
client_id: google_client_id,
|
|
callback: handleCredentialResponse
|
|
});
|
|
|
|
// Display the One Tap prompt
|
|
google.accounts.id.prompt();
|
|
|
|
// Display the Sign In With Google Button
|
|
google.accounts.id.renderButton(
|
|
document.getElementById(customBtnId),
|
|
{ theme: 'outline', size: 'large' }
|
|
);
|
|
}
|
|
|
|
function backendSignIn(id_token, firstName, lastName) {
|
|
$.ajax({
|
|
url: prestashop.urls.pages.order,
|
|
type: 'POST',
|
|
cache: false,
|
|
dataType: "json",
|
|
data: "&ajax_request=1&action=socialLoginGoogle" +
|
|
"&id_token=" + id_token +
|
|
"&firstname=" + firstName +
|
|
"&lastname=" + lastName +
|
|
"&static_token=" + prestashop.static_token,
|
|
success: function (jsonData) {
|
|
if (jsonData.hasErrors) {
|
|
// TODO: better error handling
|
|
console.error(jsonData.errors);
|
|
} else if ('undefined' !== typeof jsonData.email && jsonData.email) {
|
|
location.reload();
|
|
}
|
|
},
|
|
error: function(jqXHR, textStatus, errorThrown) {
|
|
if(jqXHR.status === 500) {
|
|
console.error("Internal server error occurred: ", errorThrown);
|
|
// Add any additional error handling logic here
|
|
}
|
|
location.reload();
|
|
}
|
|
});
|
|
}
|
|
|
|
return {
|
|
init: init
|
|
}
|
|
}());
|