使用react native。
在我的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 :(得分: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);
}
});
});