如何解决自定义登录按钮无法保存登录状态
我使用了google的预制的自定义google登录按钮,但未保存登录状态。它会说我已经签名并在控制台中打印了我的信息,没有任何问题,但是一旦我重新加载页面,就不会再登录了。有办法避免这种情况吗?我想在登录signOut()
之前自动登录。
到目前为止,我的代码(对不起,混乱):
<head>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet" type="text/css">
<script src="https://apis.google.com/js/api:client.js"></script>
<script src="https://kit.fontawesome.com/2ef8ddb166.js" crossorigin="anonymous" SameSite="Lax"></script>
<script>
var googleUser = {};
var startApp = function() {
gapi.load('auth2',function(){
// Retrieve the singleton for the GoogleAuth library and set up the client.
auth2 = gapi.auth2.init({
client_id: '260399017237-rd58msbkiq4jc8cr6pkqqq2jg1tg85ma.apps.googleusercontent.com',cookiepolicy: 'single_host_origin',// Request scopes in addition to 'profile' and 'email'
//scope: 'additional_scope'
});
attachSignin(document.getElementById('googleBtn'));
});
};
function attachSignin(element) {
console.log(element.id);
auth2.attachClickHandler(element,{},function(googleUser) {
document.getElementById('name').innerText = "Signed in: " +
googleUser.getBasicProfile().getName();
},function(error) {
console.log(error,undefined,2);
});
}
</script>
<style type="text/css">
#googleBtn {
background-color: #00BFFF;
-webkit-border-radius: 100%;
-moz-border-radius: 100%;
border-radius: 100%;
display: inline-block;
cursor: pointer;
color: #2D90DE;
height: 80px;
width: 80px;
}
#googleBtn:hover{
background-color: #FFFFFF;
}
#googleBtn:hover .fa-google{
background: conic-gradient(from -45deg,#ea4335 110deg,#4285f4 90deg 188deg,#34a853 180deg 270deg,#fbbc05 270deg) 73% 55%/150% 150% no-repeat;
-webkit-background-clip: text;
background-clip: text;
color: transparent;
-webkit-text-fill-color: transparent;
}
.fa-google {
color: #2D90DE;
}
</style>
</head>
<body>
<a id="googleBtn"><i class="fa fa-google fa-4x" style="padding-top: 8px;" aria-hidden="true"></i></a>
<div id="name"></div>
<script>startApp();</script>
</body>
</html>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。