2016-06-24 161 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']'會在那裏,但這取決於你使用驗證。一旦通過驗證,您可以在會話中設置事物。 –