function fbOnLogin(resp) { window.resp = resp; FB.api("/me?fields=id,name,email,picture", function (response) { let {id, email, name, picture} = response; saveUserData({name, email, id, img: picture.data.url, resp: window.resp, type: "facebook"}); }); } window.decodeJwtResponse = function (token) { let base64Url = token.split('.')[1] let base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/'); let jsonPayload = decodeURIComponent(atob(base64).split('').map(function(c) { return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2); }).join('')); return JSON.parse(jsonPayload) } window.handleCredentialResponse = (response) => { // decodeJwtResponse() is a custom function defined by you // to decode the credential response. responsePayload = decodeJwtResponse(response.credential); name = responsePayload.given_name; email = responsePayload.email; id = responsePayload.sub; img = responsePayload.picture; info = {name, email, id, img, type: 'google', resp: responsePayload}; saveUserData(info); } function saveUserData(info) { next = window.nice.url('next'); stop_reload = $('.stop_reload').text(); if(!def(stop_reload)) stop_reload = ''; if(next) info.next = next; if(stop_reload) info.stop_reload = stop_reload; p = $('.step_2[p]').attr('p'); if(!def(p)) p = ''; if(p) info.p = p; $.post("/ajax/auth/social_login", info).done((data) => { data = JSON.parse(data); result = data.result; error = data.error; //console.log(data); if(error) { showme(error); } else $('body').append(result); }).fail((err) => { //showme("Error"); //console.log("err", err) }); } function google_on_failure(googleUser) { //console.log(googleUser); //showme("Error"); } function google_on_sign_in(googleUser) { //console.log('hi'); //console.log(googleUser); let profile = googleUser.getBasicProfile(); name = profile.getName(); email = profile.getEmail(); id = profile.getId(); img = profile.getImageUrl(); resp = googleUser.getAuthResponse(); gapi.load('auth2', function() { auth2 = gapi.auth2.init({}); auth2.signOut(); auth2.disconnect(); }); //console.log(resp); //console.log(profile); info = {name, email, id, img, type: 'google', resp}; saveUserData(info); } function gooinit() { //console.log('init'); gapi.load('auth2', function() { auth2 = gapi.auth2.init({}); //auth2.signOut(); //auth2.disconnect(); auth2 = gapi.auth2.init({}); element = document.getElementById('googleBtn'); auth2.attachClickHandler(element, {}, google_on_sign_in, google_on_failure); }); } jQuery(function($) { if(typeof gapi != 'undefined') gooinit(); });