Last active
April 3, 2020 19:10
-
-
Save Verthon/6a0aef36f9bf1ffdafb6daf00860baf9 to your computer and use it in GitHub Desktop.
Ionic4 React Firestore problem with setting protected route for the '/account'
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// useAuth.ts | |
export default () => { | |
const dispatch = useDispatch() | |
const currentUser = useSelector(selectCurrentUser) | |
console.log('current User in useAuth hook', currentUser) | |
useEffect(() => { | |
const setUser = (user: any) => { | |
if(user) { | |
dispatch(login({ uid: user.uid, email: user.email })) | |
} else { | |
dispatch(logout()) | |
} | |
} | |
const unsubscribe = auth.onAuthStateChanged(setUser) | |
return () => unsubscribe() | |
}, [dispatch]) | |
return currentUser | |
} | |
//Root App component | |
const currentUser = useAuthUser() | |
console.log('current User in App', currentUser) // => Logs the value null, so I assume that useAuthUser is okay. | |
<Route path={ACCOUNT} exact={true} render={(props: any)=> | |
currentUser !== null || currentUser !== undefined ? ( | |
<Account {...props} /> ) : ( | |
<SignIn /> ) } /> | |
<IonTabButton tab="account" href={ACCOUNT}> | |
<IonIcon icon={accountBoxIcon} /> | |
<IonLabel>My account</IonLabel> | |
</IonTabButton> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment