如果未经Google OAuth授权后,请重定向到特定页面

时间:2019-01-15 22:26:55

标签: php google-oauth2

我让Google登录正常运行,但是不知道如何只允许数据库中已有用户。我试图仅允许Google电子邮箱位于const obj = {"a":"1", "b":"2"}; const stringToWrite = JSON.stringify(obj, null, ' ') // Add a space after every key, before the `:`: .replace(/: "(?:[^"]+|\\")*",?$/gm, ' $&'); console.log(stringToWrite);表的Mail列中的用户。如果用户不在表中,请重定向到MyTable。现在,如果我不检查数据库,则登录正常。

我认为我有两个概念问题:

1)不知道重定向是在unauthorized.html还是data-redirecturi="http://MyPage.php"

2)如果用户的电子邮件不在数据库中,我不知道如何重定向到window.location.replace("http://MyPage.php");

html:

unauthorized.html

signin.php:

<div class="WelcomeContainer">
  <div class="g-signin2" data-onsuccess="onSignIn" data-theme="dark" data-redirecturi="http://MyPage.php"></div>
</div>
<script>
function onSignIn(googleUser) {
  var id_token = googleUser.getAuthResponse().id_token;
  var xhr = new XMLHttpRequest();
  xhr.open('POST', 'signin.php');
  xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xhr.onload = function() {
      window.location.replace("http://MyPage.php");
  };
  xhr.send('idtoken=' + id_token);      
}
</script>

1 个答案:

答案 0 :(得分:1)

以下代码似乎有效。

在html中,我替换为:

xhr.onload = function() {
    window.location.replace("http://MyPage.php");
};

与此:

xhr.onload = function() {
    if (xhr.readyState == 4 && xhr.status==200) {
        window.location.replace(xhr.responseText);
    }
};

在signin.php中

if ($Test==true) {
  echo "http://www.example.com/TheGoodPage.html";
}
else {
  echo "http://www.example.com/TheUnAuthorizedPage.html";
}