diff --git a/components/Authentication.js b/components/Authentication.js index fc80c94..4568e0e 100644 --- a/components/Authentication.js +++ b/components/Authentication.js @@ -1,7 +1,7 @@ const { useEffect, useRef } = require('react'); const { useMiddleEnd } = require('strange-middle-end'); const { useSelector } = require('react-redux'); -const { useIsFocused, useNavigation } = require('@react-navigation/native'); +const { useIsFocused, useNavigation, useRoute } = require('@react-navigation/native'); const LoadingOverlay = require('components/LoadingOverlay'); exports.withAuthentication = function withAuthentication(Component) { @@ -14,20 +14,27 @@ exports.withAuthentication = function withAuthentication(Component) { const isAuthenticationSettled = useSelector(m.selectors.auth.getHasAuthenticationSettled); const navigation = useNavigation(); const isFocused = useIsFocused(); + const route = useRoute(); useEffect(() => { if (!isAuthenticated && isAuthenticationSettled && isFocused) { // not authenticated, force login if (!attemptingRef.current) { attemptingRef.current = true; - navigation.navigate('/login'); + + navigation.navigate( + '/login', + { + prev: route.name + } + ); } else { attemptingRef.current = false; navigation.canGoBack() ? navigation.goBack() : navigation.navigate('/home'); } } - }, [isAuthenticated, isAuthenticationSettled, isFocused, navigation]); + }, [isAuthenticated, isAuthenticationSettled, isFocused, navigation, route]); if (!isAuthenticationSettled && !isAuthenticated) { // authenticating from unauthenticated (doesn't cover reauthorizing or fetchCurrentUser calls) return (); diff --git a/routes/auth/containers/Login.js b/routes/auth/containers/Login.js index 3213bfd..74b4092 100644 --- a/routes/auth/containers/Login.js +++ b/routes/auth/containers/Login.js @@ -28,7 +28,15 @@ module.exports = function LoginContainer(props) { } } else { - navigation.navigate('/demo'); + const routes = navigation.getState().routes; + const loginRoute = routes.find(({ name }) => name === '/login'); + + if (loginRoute.params?.prev) { + navigation.navigate(loginRoute.params.prev); + } + else { + navigation.navigate('/demo'); + } } }; diff --git a/routes/protected/components/Protected.js b/routes/protected/components/Protected.js index d60e361..32cd60b 100644 --- a/routes/protected/components/Protected.js +++ b/routes/protected/components/Protected.js @@ -5,7 +5,7 @@ const { Button, Text } = require('@ui-kitten/components'); const internals = {}; -module.exports = function Protected({ navigation, ...props }) { +module.exports = function Protected({ onPressLogout, onPressDemo, ...props }) { const { CenteredButton, BigText, Wrapper } = internals; @@ -13,15 +13,16 @@ module.exports = function Protected({ navigation, ...props }) { This page is exclusive to logged-in users. - navigation.navigate('/demo')}>Go to Demo - navigation.navigate('/login')}>Log Out + Go to Demo + Log Out ); }; module.exports.propTypes = { - navigation: T.object.isRequired + onPressLogout: T.func.isRequired, + onPressDemo: T.func.isRequired }; internals.BigText = Styled(Text)` diff --git a/routes/protected/containers/Protected.js b/routes/protected/containers/Protected.js index 105c905..75013f7 100644 --- a/routes/protected/containers/Protected.js +++ b/routes/protected/containers/Protected.js @@ -9,13 +9,9 @@ module.exports = function ProtectedContainer(props) { const onPressLogout = async () => { - try { - await m.dispatch.auth.logout(); - } - finally { - navigation.navigate('login'); - } + await m.dispatch.auth.logout(); + navigation.navigate('/login'); }; - return (); + return ( navigation.navigate('/demo')} />); };