Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logout #9

Open
MarshallPatryk opened this issue Nov 10, 2020 · 4 comments
Open

Logout #9

MarshallPatryk opened this issue Nov 10, 2020 · 4 comments

Comments

@MarshallPatryk
Copy link

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
Copy link

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

@moafzalmulla
Copy link

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
Copy link

moafzalmulla commented Nov 27, 2020

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
Copy link

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants