0

I am trying to implement a Login and Logout functionality. Everything is working fine, but login in after showing home screen it's showing not show the logout button but after the app opens and closes shows the logout button.

   const [isLoggedIn, setIsLoggedIn] = useState(false);
   useEffect(() => {
        getTokenData()

    }, [])

    const getTokenData = async () => {
        const token = await AsyncStorage.getItem('access_token');
        setIsToken(token)
        console.warn(token)
        if (token !== null) {
            setIsLoggedIn(true)
            console.warn('logout true')
        }
        else {
            console.warn('logout Success')
        }
    };

    const handleSignIn = () => {
       
        setIsLoggedIn(false)
        console.warn('sign in')
        navigation.navigate('SignIn')
    }
    const handleLogout = async () => {
        await AsyncStorage.removeItem('access_token')
        setIsLoggedIn(false)
        console.warn('logout')
        navigation.navigate('HomePage')
    }

  {isLoggedIn ?
   <Btn btnLabel={'Logout'} onPress={handleLogout} />:
   <Btn btnLabel={'Sign In'} onPress={handleSignIn} /> 
 }
ram
  • 437
  • 2
  • 12
  • did you try adding `setIsLoggedIn(true)` at the end of `handleSignIn( )`? – ram Apr 22 '23 at 07:03
  • can you update your question writing which page does the code you wrote belongs to? It's also unclear which flow the logout button is not being rendered. – VariabileAleatoria Apr 23 '23 at 08:15
  • https://stackoverflow.com/questions/70622599/useeffect-is-not-called-when-navigating-back-to-screen-react-navigation – Raja Kumar Apr 23 '23 at 12:55

1 Answers1

0

I have answered similar question in this link. Auth Workflow - Persistent Login

If you are using axios , redux it will be helpful for you.

Alok Prakash
  • 191
  • 1
  • 9
  • useEffect(() => { if (isFocused) { getTokenData(); } // console.warn("islofgin chages") }, [isLoggedIn, isFocused]) – Raja Kumar May 09 '23 at 12:08