<script async defer src="https://apis.google.com/js/api.js" onload="this.onload=function(){};HandleGoogleApiLibrary()" onreadystatechange="if (this.readyState === 'complete') this.onload()"></script>

<script>

 

// Called when Google Javascript API Javascript is loaded
function HandleGoogleApiLibrary() {
// Load "client" & "auth2" libraries
gapi.load('client:auth2', {
callback: function() {
// Initialize client & auth libraries
gapi.client.init({
apiKey: 'rwRD7JaafrbftsfzuuhuiyZb',
clientId: '336824978877-4lsqhdejkstrydkbs1lv3m9lr0itlpdo',
scope: 'https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/plus.me'
}).then(
function(success) {
// Libraries are initialized successfully
// You can now make API calls
},
function(error) {
// Error occurred
// console.log(error) to find the reason
}
);
},
onerror: function() {
// Failed to load libraries
}
});
}

 

    

// Call login API on a click event
$('document').ready(function(){
$("#google-login-button").on('click', function() {
debugger;
// API call for Google login
gapi.auth2.getAuthInstance().signIn().then(
function(googleUser) {
// Login API call is successful
var profile = googleUser.getBasicProfile();

var formData = {
'id': profile.getId(),
'full_name': profile.getName(),
'given_name': profile.getGivenName(),
'family_name': profile.getFamilyName(),
'image_url': profile.getImageUrl(),
'email': profile.getEmail(),
'login_type':'google'
};

$.ajax({
type:'POST',
url: siteurl+"requests/do_login.php",
data:formData,
success:function(data){
if($.trim(data) === 'user_error'){

} else if($.trim(data) === 'login_ok'){
//setTimeout function added to see results.
setTimeout(function() {

setTimeout(function() {
window.location.href = siteurl;
}, 1000);

submit.html('');
}, 2000);
} else{

console.log(data);
}
},

error: function(data){
login_error.show().html('Something went wrong, try again');
},
});
console.log(formData);
console.log(gapi);

},
function(error) {
// Error occurred
// console.log(error) to find the reason
}
);
});
});

</script>