instamobile / react-native-firebase

React Native Firebase Starter Project with Auth, Firestore, Storage and Push Notifications
https://instamobile.io
MIT License
411 stars 136 forks source link

Logout #9

Open MarshallPatryk opened 3 years ago

MarshallPatryk commented 3 years ago

Hi, how can I remove state and logout user from this project? I tried: const signOut = async () => { try { await firebase.auth().signOut(); navigation.navigate("Login"); } catch (e) { console.log(e); } } but state wasn't removed

anthonyflores-github commented 3 years ago

Hello, have you found the solution? I'm also looking for how to disconnect.

moafzalmulla commented 3 years ago

So I saw a stack overflow saying I should use redux, then came across a documentation saying redux is ok for state, but not Navigation state.

I wrote a logout function as: const onLogoutButtonPress = () => { // navigation.navigate('Login'); firebase .auth() .signOut() .then(() => { console.log("USER LOGGED IN !!!: " + JSON.stringify(user)) setUser(null) console.log("USER LOGGED OUT !!!: " + JSON.stringify(user)) // navigation.navigate('Login')} // navigation.navigate( // 'Home', // {}, // NavigationActions.navigate({ // routeName: 'HomeScreen' // }) // ) }) .catch(error => { alert(error) }) }

That logs out user, but doesnt re-render app.js which houses user auth logic.

I looked react navigation documentation, but to no avail. Maybe I missed something

Some sources say you can use NavigationActions , as navigation is not available for use as navigation.navigate('Login')}

That said I passed navigation as a prop and it was empty. Thats because navigation.navigate() is only available to navigate to pages within the stack.

In our case, HomeScreen is in one stack and Login + Registration is in another stack. So we cannot move up from stack to stack.

But I wonder if we put all of them in the same stack, then navigation.navigate() will work right and being a mobile app, the user wont be able to navigate to the page without a button provided by us ? ....hmmm this just came to me #rubberDuckTheory .. I will test and get back to you guys.

PS. I am not using redux for this, but might keep it in to manage other all round state bits.

If any one else gets a solution, do post #happyLockdownCoding

Mo

moafzalmulla commented 3 years ago

UPDATE:

So I did 2 things and I think it works.

  1. add a copy of Login and Registration components in Logged in NavigationStack: Screenshot 2020-11-27 at 18 18 16

  2. Add this logout function: Screenshot 2020-11-27 at 18 18 34

It seems to now be logging out for me and loads the component as the Login and Registration components are now in the stack. By the time it calls that component firebase .signOut() has already run

So in effect, you wont be logged in anymore

Try and let me know

Hope this helps

@mrcflorian Thank you for this awesome repo. Will be good to hear your thoughts.

Sources: https://reactnavigation.org/docs/stack-actions/

PingOfD32th commented 3 years ago

code

this should work for any future googlers. setting setUser to null triggers a rerender and thus you to automatically go to the login screen, so there is no need to redirect explicitly (unless you wanted to make a "logged out" screen).