1

I am trying to implement FB login functionality within my app using react native. One of the issues I am facing is that when I check to see if i have fb token stored, the initialization of the react navigator does not wait for the function to complete. I know this because I see in my debugger statements appearing for navigation initialization before my isLoggedIntoFB() returning a result. I am confused because I thought I was calling my functions in a synchronous way.

auth/index.js:

const FBSDK = require('react-native-fbsdk');
const {
  AccessToken,
    LoginManager,
} = FBSDK;
export const isLoggedIn = () => {

if(isLoggedIntoFB()) {
    return true;
} else {
    // returning false even though isLoggedIntoFB() prints "tokenFound" 
    // statement later
    return false;
}
}

export const OnLogout = () => {
 alert("Logging out");
logoutFB();
}



function isLoggedIntoFB() {
    alert("isLoggedIn");
AccessToken.getCurrentAccessToken().then(
            (data) => {
                console.log(data);
            if(data.accessToken != null){
                console.log("Token Found")
                return true;
            } else {
                console.log("No previous token found")

                return false;
            }


            }
        );
}

logoutFB = () => {
LoginManager.logOut();
//AccessToken.setCurrentAccessToken(null);
}

reducer/index.js:

import { combineReducers } from 'redux';
import { NavigationActions } from 'react-navigation';
import { AppNavigator } from '../navigators/AppNavigator';
import { isLoggedIn } from "../auth";



var initialNavState = (function() {
if(isLoggedIn() ) { // immediately evaluates to false
    console.log("LoggedIn!!!");
return AppNavigator.router.getStateForAction(NavigationActions.reset({
    index: 0,
    actions: [
      NavigationActions.navigate({
        routeName: 'LoggedIn',
      }),
    ],
}));
} else {
    console.log("not Logged In");
return AppNavigator.router.getStateForAction(NavigationActions.reset({
  index: 1,
  actions: [
    NavigationActions.navigate({ routeName: 'LoggedIn'}),
    NavigationActions.navigate({ routeName: 'LoggedOut'})
  ]
}));
}
}
) ();




function nav(state = initialNavState, action) {
  let nextState;
  switch (action.type) {
    case 'LOGIN':
      nextState = AppNavigator.router.getStateForAction(
        NavigationActions.back(),
        state
      );
      break;

    case 'LOGOUT':
      nextState = AppNavigator.router.getStateForAction(
        NavigationActions.navigate({ routeName: 'LoggedOut' }),
        state
      );
      break;
    default:
      nextState = AppNavigator.router.getStateForAction(action, state);
      break;
  }

  // Simply return the original `state` if `nextState` is null or undefined.
  return nextState || state;
}
john
  • 1,057
  • 1
  • 17
  • 28
  • you could use callback then. just passed the next function to the prerequisite function. Just call the callback function after you finished what you need. – elegisandi Jul 30 '17 at 01:51
  • `isLoggedIntoFB` is asynchronous - it makes a request that gets data later, not immediately, you have to wait for the response - it can't return `true` or `false` synchronously (i.e. immediately) - dupe of the all time favourite SO dupe [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Adam Jenkins Jul 30 '17 at 01:58
  • can you show how I would do that? – john Jul 30 '17 at 04:14

0 Answers0