繁体   English   中英

使用Google登录表单登录后,如何将用户重定向到新页面?

[英]How can I redirect users to a new page after they logged in with the Google login form?

我正在为我的网络应用使用google登录按钮。 登录后,我想将已登录的用户重定向到新页面。 我似乎无法使重定向工作。

作为替代,我制作了一个setTimeOut函数,该函数在按下登录按钮时触发。 因此,几秒钟后,用户将被重定向。 但是,当网络有点延迟时,即使登录尚未完成,用户也会被重定向。

我还尝试了一条if else语句,如果登录成功,则可以触发setTimeOut函数。 但这似乎也不起作用。 我是JS的新手,所以对于任何明显的错误我深表歉意。

  <script>
     function delayFunction() {
       setTimeout("location.href = 'here comes the redirect url';");
     }

     function doNothing() {

     }
  </script>

  <body>
    <div class="container">
        <div class="user"><a href="/pages/profile.php"><p id="name"></p></a></div>
            <div class="headtitle">
                <div class="loginpage">
                  <div class="g-signin2" data-onsuccess="onSignIn" onclick="setTimeout(delayFunction, 3500)"></div>
                    <div class="of"><p>Or:</p></div>
                      <div class="buttonWebsite">Go to our website</div>

   <script>
      if (onSignIn = true) {
       setTimeout(delayFunction, 3500)
      } else {
       doNothing()
      }
    </script>

如果要运行if语句,则删除onclick =“ setTimeOut”进行测试,等等。

查看数据属性data-onsuccess功能。 它将寻找一个名为onSignIn的函数(按其值指定)并执行

做一个这样的功能

<script>
      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;
        console.log("ID Token: " + id_token);

      window.location = '<redirect_url>'
      }
    </script>

查看此链接以获取更多信息: https : //developers.google.com/identity/sign-in/web/

在后端验证生成的令牌: https : //developers.google.com/identity/sign-in/web/backend-auth

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM