1

使用反應原生。如何檢查用戶是否使用Facebook登錄

在我的JSX代碼中,我處理了onLoginFound事件,並說如果找到登錄名就打印出來。這永遠不會打印這是奇怪的。

我加入這行想我可以使用getCurrentAccessToken()函數來檢查,如果用戶登錄。

alert(AccessToken.getCurrentAccessToken() != null) 

雖然即使沒有人因此被記錄此返回true,所有的時間似乎沒有工作。

有什麼明顯的我失蹤了嗎?不能相信檢查用戶是否登錄可能是我想要的一個痛苦點。

所有我下面的代碼:

import React, { Component } from 'react'; 
import { 
    AppRegistry, 
    StyleSheet, 
    Text, 
    View 
} from 'react-native'; 

const FBSDK = require('react-native-fbsdk'); 
const { 
    LoginButton, 
    AccessToken, 
    GraphRequest, 
    GraphRequestManager, 
} = FBSDK; 

export default class FBReact extends Component { 
    render() { 
    return (
     <View style={styles.container}> 
     <Text style={styles.welcome}> 
      Welcome to React Native! 
     </Text> 
     <Text style={styles.instructions}> 
      To get started, edit index.ios.js 
     </Text> 
     <Text style={styles.instructions}> 
      Press Cmd+R to reload,{'\n'} 
      Cmd+D or shake for dev menu 
     </Text> 
     <LoginButton 
      publishPermissions={["publish_actions"]} 
      onLoginFinished={ 
      (error, result) => { 
       if (error) { 
       alert("login has error: " + result.error); 
       } else if (result.isCancelled) { 
       alert("login is cancelled."); 
       } else { 
       AccessToken.getCurrentAccessToken().then(
        (data) => { 
        alert(data.accessToken.toString()); 
        let token = data.accessToken.toString(); 
        } 
       ) 
       } 
      } 
      } 
      onLogoutFinished={() => alert("logout.")} 
      onLoginFound={function(data){ 
      console.log("Existing login found."); 
      console.log(data); 
      alert("log in found"); 
      _this.setState({ user : data.credentials }); 
      }}/> 
     </View> 
    ); 
    } 
} 

const styles = StyleSheet.create({ 
    container: { 
    flex: 1, 
    justifyContent: 'center', 
    alignItems: 'center', 
    backgroundColor: '#F5FCFF', 
    }, 
    welcome: { 
    fontSize: 20, 
    textAlign: 'center', 
    margin: 10, 
    }, 
    instructions: { 
    textAlign: 'center', 
    color: '#333333', 
    marginBottom: 5, 
    }, 
}); 

AppRegistry.registerComponent('FBReact',() => FBReact); 
alert(AccessToken.getCurrentAccessToken() != null) 
+0

當你'的console.log(AccessToken.getCurrentAccessToken())'你得到了什麼?可能是你真的登錄了,你需要在accesstoken變爲null之前註銷。 在我的代碼中,我通過在成功Facebook登錄後在AsyncStorage中存儲了一些東西。然後再檢查存儲的值。 – cjmling

+0

AccessToken.getCurrentAccessToken()似乎只是返回一個對象,無論我是否登錄或不。不知道是否相關,但每當我關閉應用程序並重新打開它時,登錄按鈕會切換回「使用Facebook登錄」,這意味着當我關閉應用程序時,我會自動退出。僅供參考,您如何使用存儲在AsyncStorage中的令牌來處理Facebook? –

+0

基本上與FacebookLogin我只需要電子郵件和一些其他配置文件數據。所以我使用了LoginManager https://github.com/facebook/react-native-fbsdk#requesting-additional-permissions-with-login-manager。一旦我得到這些用戶數據。然後,只需使用AsyncStorage或任何其他相關數據將這些電子郵件存儲起來以供參考,或發送給您正在運行的任何後端身份驗證服務器。 – cjmling

回答

0

AccessToken.getCurrentAccessToken()是一個承諾。

您需要等待它才能解析以獲取內容。具體如下:

AccessToken.getCurrentAccessToken() 
    .then((tokenInfo) => { 
    console.log(tokenInfo); 
    }); 

下面是這個方法的參考源代碼:

/** 
* Getter for the access token that is current for the application. 
*/ 
static getCurrentAccessToken(): Promise<?FBAccessToken> { 
    return new Promise((resolve, reject) => { 
    AccessToken.getCurrentAccessToken((tokenMap) => { 
     if (tokenMap) { 
     resolve(new FBAccessToken(tokenMap)); 
     } else { 
     resolve(null); 
     } 
    }); 
    });