Skip to content

Instantly share code, notes, and snippets.

@Verthon
Last active April 3, 2020 19:10
Show Gist options
  • Save Verthon/6a0aef36f9bf1ffdafb6daf00860baf9 to your computer and use it in GitHub Desktop.
Save Verthon/6a0aef36f9bf1ffdafb6daf00860baf9 to your computer and use it in GitHub Desktop.
Ionic4 React Firestore problem with setting protected route for the '/account'
// 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