Vin*_*ent 10 google-api google-plus google-signin
我使用Google Plus作为登录,直到最近才运行良好.现在用户无法再退出.回调工作并返回用户已注销的内容,但之后会立即再次签署用户.好像它没有存储注销.
有一些老问题就像这个问题.我尝试了所有提议的解决方案但没有任何效果
html头中的代码
<script src="https://apis.google.com/js/client:platform.js?onload=start" async defer></script>
Run Code Online (Sandbox Code Playgroud)
按钮代码,始终显示(登录时隐藏)
<div id="signinButton">
<span class="g-signin"
data-scope="https://www.googleapis.com/auth/gmail.readonly"
data-clientid="{{ CLIENT_ID }}"
data-redirecturi="postmessage"
data-accesstype="offline"
data-cookiepolicy="single_host_origin"
data-callback="signInCallback">
</span>
</div>
Run Code Online (Sandbox Code Playgroud)
SignIn和SignOut函数
<script>
function signInCallback(authResult) {
//console.log(authResult)
if (authResult['code']) {
var state = encodeURIComponent('{{ STATE }}');
var code = encodeURIComponent(authResult['code']);
var tz = encodeURIComponent($('#timezone').val());
var cntry = encodeURIComponent($('#country').val());
var lan = encodeURIComponent('{{ language }}');
// Send the code to the server
$.ajax({
type: 'POST',
url: '/signup/gauth',
contentType: 'application/octet-stream; charset=utf-8',
success: function(result) {
console.log(result)
if (result == 'Success') {
{% if not user %}window.location = "/user/home";{% else %}
console.log('Logged in');{% endif %}
}
},
processData: false,
data: 'code='+code+'&state='+state+'&country='+cntry+'&tz='+tz+'&language='+lan
});
}
else if (authResult['error']) {
console.log('Sign-in state: ' + authResult['error']);
}
}
function signOut() {
gapi.auth.signOut();
window.location = "/user/logout"
}
</script>
Run Code Online (Sandbox Code Playgroud)
编辑:我使用两步方法实现完全注销和注销:首先我注销,然后关闭当前页面并打开一个注销php页面来终止当前会话(我可以使用Ajax,但我更喜欢在注销后将用户发送到主页,那为什么还要麻烦呢?)。
<head>
<meta name="google-signin-scope" content="profile email">
<meta name="google-signin-client_id" content="your-CLIEntID">
<script src="https://apis.google.com/js/platform.js" async defer>
</script>
<script>
function signOut() {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut().then(function () {
console.log('User signed out.');
});
}
</script>
</head>
<body>
...
<a href='logout.php' onclick='signOut();'>LOGOUT</a>
...
Run Code Online (Sandbox Code Playgroud)
这是我的退出(生产),对于最终版本,我建议您删除控制台日志记录
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut();
Run Code Online (Sandbox Code Playgroud)
注销.php
<?php
ob_start();
session_start();
$serverName = $_SERVER['SERVER_NAME'];
$tokenGoogle = $_POST['myTokenFromGoogle'];
if ($tokenId) {
debug_to_console("inside LOGOUT has tokenGoogle [$tokenGoogle]");
$url = "https://accounts.google.com/o/oauth2/revoke?token=$tokenGoogle";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$json = json_decode($response, true);
curl_close($ch);
header("Location: http://$serverName/pageThatConsumesdata.php?".implode("|",$json));
} else {
debug_to_console("inside LOGOUT HAVING NO tokenGoogle");
if(isset($PHPSESSID)) {
$message = "time for a change of ID? ($PHPSESSID).";
$sessionName = session_id();
session_regenerate_id();
$sessionName2 = session_id();
} else {
$message = "There was no session to destroy!";
}
debug_to_console($message);
debug_to_console("[$serverName]");
session_destroy();
header("Location: http://".$serverName);
exit;
}
function debug_to_console( $data ) {
if ( is_array( $data ) ) {
$output = "<script>console.log( '" . implode( ',', $data) . "' );</script>";
} else {
$output = "<script>console.log( '" . $data . "' );</script>";
}
echo $output;
}
?>
Run Code Online (Sandbox Code Playgroud)
注意:出于调试目的,我添加了一个从 php 打印到控制台的功能(单击 SHIFT+CTRL+J 在 Firefox 或 Chrome 中查看控制台)。这绝不是这样做的标准做法,一旦最终代码启动,就应该将其删除。
| 归档时间: |
|
| 查看次数: |
3359 次 |
| 最近记录: |