0

I call this function:

let isAuthed = checkIfAuthed(store)

this is the function checkIfAuthed :

function checkIfAuthed (store) {
  // debugger
  // const user = firebase.auth().currentUser
  return store => {
  firebase.auth().onAuthStateChanged((user) => {
    // console.log('isAuthed from on state changed', user)
    // debugger
    if (user === null) {
      debugger
      alert('user==null')
      return false
    } else if ((store.getState().isAuthed === false) && (user !== null)) {
      console.log('user', user.displayName)
        // debugger
        // const userInfo = formatUserInfo(user.displayName, user.photoURL, user.uid)
        // // debugger
        // store.dispatch(authUser(user.uid))
        // // debugger
        // store.dispatch(fetchingUserSuccess(user.uid, userInfo))
        // debugger
      alert('user==exists & redux store had it false')
      return true
    } else {
      debugger
      alert('user== exists at both places')
      return true
    }
  })
  }
}

The isAuthed variable is now always a function. I am expecting false or true. What am I doing wrong??

jasan
  • 11,475
  • 22
  • 57
  • 97
  • `checkIfAuthed` doesn't actually have a return statement at all. – Quentin Jun 15 '16 at 20:00
  • Only the inner function `firebase.auth().onAuthStateChanged` returns something. The outer `checkIfAuthed` doesn’t return anything. You need to write `return firebase.auth().onAuthStateChanged((user) => {`…`}`, although I’m not sure whether you need to pay attention to asynchronous calls. – Sebastian Simon Jun 15 '16 at 20:00
  • @Xufox adding return statement to firebase.auth().onAuthStateChanged() return a function as the value of isAuthed not a boolean – jasan Jun 15 '16 at 20:08
  • @jasan Hold on a second… your original question didn’t have a `store => {`…`}`. Now, of course it will return a function. – Sebastian Simon Jun 15 '16 at 20:40
  • @Xufox, thats the change i made after your comment – jasan Jun 15 '16 at 20:48
  • @Quentin I apologize but its still unclear from the answers to the other question. I updated the question. I added the return statement but that just returns a function still not a Boolean – jasan Jun 15 '16 at 20:51
  • You can't return the value because you don't have it when the function returns. It's an asynchronous event. – Quentin Jun 15 '16 at 21:03

0 Answers0