如何检查用户是否已经登录? [英] How to check if the user is already logged in?

查看:257
本文介绍了如何检查用户是否已经登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是react-native和Facebook sdk

I am complete new to react-native and Facebook sdk

因此,我按照给出的此处进行的教程,一切正常,用户能够使用Facebook登录.

So I have followed the tutorial given here and everything works fine, the user is able to log in using facebook.

我的问题是:

当用户下次打开应用程序时,如何检查用户是否已经登录?

我认为无论用户是否已经登录,每次都会调用回调onLoginFinished.但事实并非如此.如果用户已登录,则不会调用onLoginFinished.是否有任何垂直回调来检查?或任何其他方法或一些解决方法?

I thought the callback onLoginFinished is called everytime regardless whether the user has already logged in or not. But that is not the case. If the user is logged in, the onLoginFinished not called. Is there any perticular callback to check that? or any other method or some workaround?

我试图阅读react-native-fbsdk 此处的文档,但似乎他们还不够.

I tried to read the docs of react-native-fbsdk here but it seems they are not enough.

这是登录"按钮:

<LoginButton
    onLoginFinished={ (error, result) => { this.handleLogIn(error, result) } }
    onLogoutFinished={() => { this.handleLogOut() } } 
/>

助手功能:

handleLogIn(error, result) {
    if (error) {
        alert("Login failed with error: " + result.error);
    } else if (result.isCancelled) {
        alert("Login was cancelled");
    } else {
        alert("Login was successful")
        AccessToken.getCurrentAccessToken().then(
            (data) => {console.log(data);} //Refresh it every time
        );
    }
}

handleLogOut() {
    console.log("User logged out");
    alert("Log out successful");
}

推荐答案

启动应用时,您需要在适当的开头检查currentAccessToken是否有效.如果返回null,则需要刷新令牌;如果刷新令牌已过期,则将用户重定向到登录页面.

You need to check if currentAccessToken is valid in the beginning somewhere appropriate when your app is started. If it returns null, you need to refresh the token and if refresh token is expired, redirect user to loginpage.

AccessToken.getCurrentAccessToken().then(
            (data) => {console.log(data);} //Refresh it every time
        );

这篇关于如何检查用户是否已经登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆