简体   繁体   中英

How to check if a user is logged in with Facebook

Using react native.

In my JSX code i threw in handling for the onLoginFound event, saying to print something if a login was found. This NEVER prints which is weird.

I then added this line thinking I could use the getCurrentAccessToken() function to check if the user is logged in.

alert(AccessToken.getCurrentAccessToken() != null)

This returns true all the time though even when no one is logged in so it doesn't appear to be working.

Is there something obvious I am missing? can't believe checking if a user is logged in could be such a pain point for me to figure out.

All my code below:

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)

AccessToken.getCurrentAccessToken() is a promise.

You need to wait it to resolve to get the content. As follows:

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

Here is the source code of this method for reference:

/**
 * 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);
      }
    });
  });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM