2017-05-30 75 views
1

我一直停留在这几个小时。我试过谷歌搜索答案,但我不明白其中的任何和必须假设它是过时的代码。谷歌登录 - 防止自动负载

我想添加一个'用google登录'到我的网站,但我希望它只能重定向到登录检查,如果用户点击按钮,而不是一旦它检测到我已经登录在

按钮:

<div class="g-signin2" data-onsuccess="onSignIn"></div> 

脚本:

<script> 
function onSignIn(googleUser) { 
    var profile = googleUser.getBasicProfile(); 
    console.log('ID: ' + profile.getId()); 
    console.log('Name: ' + profile.getName()); 
    var id_token = googleUser.getAuthResponse().id_token; 
    window.location.replace("googleLogin.phptoken="+ id_token); 
    } 
</script> 

任何人都征服了这个问题?

回答

0

我真的不能记得我做过什么来解决这个问题,但我有同样的一个最终使这里一个新的脚本是一个js拨弄一个链接,你将有自己的客户编号堵塞,它不有一个图像,所以你只会看到谷歌登录按钮的一张破碎的图片。

var googleUser = {}; 
var startApp = function() { 
gapi.load('auth2', function(){ 
    // Retrieve the singleton for the GoogleAuth library and set up the client. 
    auth2 = gapi.auth2.init({ 
    client_id: '83******98-pe4uneu******sj.apps.googleusercontent.com', 
    cookiepolicy: 'single_host_origin', 
    // Request scopes in addition to 'profile' and 'email' 
    //scope: 'additional_scope' 
    }); 
    attachSignin(document.getElementById('customBtn')); 
}); 
}; 

function attachSignin(element) { 
console.log(element.id); 
auth2.attachClickHandler(element, {}, 
    function(googleUser) { 
    first_name = googleUser.getBasicProfile().getGivenName(); 
     last_name = googleUser.getBasicProfile().getFamilyName(); 
     email = googleUser.getBasicProfile().getEmail(); 
     console.log(googleUser.getBasicProfile().getGivenName()); 
     console.log(googleUser.getBasicProfile().getFamilyName()); 
     console.log(googleUser.getBasicProfile().getEmail()); 
     window.location = "/profile"  
    }, function(error) { 
     alert(JSON.stringify(error, undefined, 2)); 
    }); 

} 

startApp(); 

https://jsfiddle.net/josephmckenzie/9pxc41kx/

所有你可能需要的代码是在爵士小提琴