2016-06-24 163 views

回答

0

你需要在回调函数中做你自己的重定向。想象一下documentation here中的代码。你需要做的是延长它像这样:

 function onSignIn(googleUser) { 
 
     // Useful data for your client-side scripts: 
 
     var profile = googleUser.getBasicProfile(); 
 
     console.log("ID: " + profile.getId()); // Don't send this directly to your server! 
 
     console.log('Full Name: ' + profile.getName()); 
 
     console.log('Given Name: ' + profile.getGivenName()); 
 
     console.log('Family Name: ' + profile.getFamilyName()); 
 
     console.log("Image URL: " + profile.getImageUrl()); 
 
     console.log("Email: " + profile.getEmail()); 
 

 
     // The ID token you need to pass to your backend: 
 
     var id_token = googleUser.getAuthResponse().id_token; 
 
     postAJAX('/server/sign-in', {id_token: id_token}) 
 
     .then(function(user) { 
 
      // The user is now signed in on the server too 
 
      // and the user should now have a session cookie 
 
      // for the whole site. 
 
      document.location.href = '/dashboard/' + user.username 
 
     }) 
 
     
 
     
 
     };

+0

我假设'/服务器/签in'会把''在$ _ POST [ 'id_token']了' $ _SESSION'超全球。那是对的吗?你是否也在会话中切换某种授权布尔值? –

+0

另外,我想你可能会在你的'postAJAX'函数调用(从底部开始的第三行)末尾丢失一个分号。 –

+0

服务器端部分由您决定。 '$ _POST ['id_token']'会在那里,但这取决于你使用验证。一旦通过验证,您可以在会话中设置事物。 –