Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/430.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何检查用户是否使用“登录”;谷歌登录“;(OAuth 2.0)_Javascript_Html_Oauth_Oauth 2.0_Google Api - Fatal编程技术网

Javascript 如何检查用户是否使用“登录”;谷歌登录“;(OAuth 2.0)

Javascript 如何检查用户是否使用“登录”;谷歌登录“;(OAuth 2.0),javascript,html,oauth,oauth-2.0,google-api,Javascript,Html,Oauth,Oauth 2.0,Google Api,我正在实现谷歌登录的第一次描述和 我正在使用HTML和Javascript 需要解决的问题如下:在初次登录后,如何在不同的页面(例如登录后用户看到的登录页面或门户)上检查用户是否登录?是否有一个服务,我可以打电话检查用户的登录状态与我的应用程序键或类似的东西? 我想我必须在每个页面上都包含GoogleAPI 登录页面代码: 头部脚本(上面列出的谷歌教程中的代码): .... 函数onSignIn(谷歌用户) { var profile=googleUser.getBasicProfile();

我正在实现谷歌登录的第一次描述和

我正在使用HTML和Javascript

需要解决的问题如下:在初次登录后,如何在不同的页面(例如登录后用户看到的登录页面或门户)上检查用户是否登录?是否有一个服务,我可以打电话检查用户的登录状态与我的应用程序键或类似的东西? 我想我必须在每个页面上都包含GoogleAPI

登录页面代码:

头部脚本(上面列出的谷歌教程中的代码):


....
函数onSignIn(谷歌用户)
{
var profile=googleUser.getBasicProfile();
log('ID:'+profile.getId());
log('Name:'+profile.getName());
log('Image URL:'+profile.getImageUrl());
log('Email:'+profile.getEmail());
警报(profile.getName());
}
函数注销()
{
警报(“注销”);
var auth2=gapi.auth2.getAuthInstance();
auth2.signOut().then(函数(){
log('User signed out');
});
}
...
正文中的代码(上面列出的谷歌教程的第一行,触发注销测试的第二行)


...
注销
...

有没有什么方法可以在另一个页面上包含GoogleAPI,然后调用一些检查登录状态的函数?或者用另一种方法来具体说明用户是登录还是注销?

您可以对自定义userEntity对象进行字符串化,并将其存储在sessionStorage中,以便在加载新页面时随时进行检查。我没有测试以下内容,但它应该可以工作(以同样的方式使用WebAPI令牌进行类似的操作)

函数onSignIn(谷歌用户)
{
var profile=googleUser.getBasicProfile();
log('ID:'+profile.getId());
log('Name:'+profile.getName());
log('Image URL:'+profile.getImageUrl());
log('Email:'+profile.getEmail());
var myUserEntity={};
myUserEntity.Id=profile.getId();
myUserEntity.Name=profile.getName();
//将实体对象存储在sessionStorage中,从站点的所有页面都可以访问该实体对象。
setItem('myUserEntity',JSON.stringify(myUserEntity));
警报(profile.getName());
}
函数checkIfLoggedIn()
{
if(sessionStorage.getItem('myUserEntity')==null){
//重定向到登录页面,sessionStorage中没有可用的用户实体
window.location.href='Login.html';
}否则{
//用户已登录
var userEntity={};
userEntity=JSON.parse(sessionStorage.getItem('myUserEntity');
...
DoWhatever();
}
}
函数注销()
{
//当用户注销时,不要忘记清除会话存储
sessionStorage.clear();

}
您不需要在本地存储上存储任何内容。该库允许您检查用户是否已登录,
gapi
对象的
auth2
上的
isSignedIn.get()

加载JavaScript库,确保没有延迟加载:

<script src="https://apis.google.com/js/platform.js"></script>

别忘了更改
应用程序id

再加上上面Joseph的答案,您可以通过调用
auth2.currentUser.get().getBasicProfile()获取有关用户的信息

if(auth2.isSignedIn.get()){
googleUserProfile=auth2.currentUser.get().getBasicProfile()
log('ID:'+googleUserProfile.getId());
log('全名:'+googleUserProfile.getName());
log('给定名称:'+googleUserProfile.getGivenName());
log('Family Name:'+googleUserProfile.getFamilyName());
log('Image URL:'+googleUserProfile.getImageUrl());
log('Email:'+googleUserProfile.getEmail());
}

从文档中:

检查用户是否已在使用中签名:

gapi.auth2.getAuthInstance().isSignedIn.get()

谢谢你的回答-我会尝试一下,一有时间就更新。你能告诉我“sessionStorage”变量是javascript中的某种全局对象吗?或者这是你必须设置的东西?每个新页面是否仍需要调用php sessionstart函数才能在javascript中访问此变量?当然。。。它在javascript中可用,您不需要引用任何第三方库。你可以从W3Schools获得更多的信息,它的等价物是什么?
<script src="https://apis.google.com/js/platform.js"></script>
var auth2;
var googleUser; // The current user

gapi.load('auth2', function(){
    auth2 = gapi.auth2.init({
        client_id: 'your-app-id.apps.googleusercontent.com'
    });
    auth2.attachClickHandler('signin-button', {}, onSuccess, onFailure);

    auth2.isSignedIn.listen(signinChanged);
    auth2.currentUser.listen(userChanged); // This is what you use to listen for user changes
});  

var signinChanged = function (val) {
    console.log('Signin state changed to ', val);
};

var onSuccess = function(user) {
    console.log('Signed in as ' + user.getBasicProfile().getName());
    // Redirect somewhere
};

var onFailure = function(error) {
    console.log(error);
};

function signOut() {
    auth2.signOut().then(function () {
        console.log('User signed out.');
    });
}        

var userChanged = function (user) {
    if(user.getId()){
      // Do something here
    }
};
gapi.auth2.getAuthInstance().isSignedIn.get()