From 9601a6331e1c531435f808a350c8bb2b5277d44a Mon Sep 17 00:00:00 2001 From: Marlon del Rosario Date: Fri, 25 Aug 2023 13:36:38 -0400 Subject: [PATCH 1/4] handle previous route when redirecting after authentication --- components/Authentication.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/components/Authentication.js b/components/Authentication.js index fc80c94..a348c64 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,28 @@ 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 + } + ); + // navigation.navigate('/login'); } 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 (); From 1c469b1de6c7ec6866f13718e132c4f83c9a915c Mon Sep 17 00:00:00 2001 From: Marlon del Rosario Date: Fri, 25 Aug 2023 13:37:43 -0400 Subject: [PATCH 2/4] handle previous route when reidrecting after authentication --- routes/auth/containers/Login.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) 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'); + } } }; From 080404e788d654409b0ef1aa009110f2a218b305 Mon Sep 17 00:00:00 2001 From: Marlon del Rosario Date: Fri, 25 Aug 2023 13:38:20 -0400 Subject: [PATCH 3/4] wire protected route --- routes/protected/components/Protected.js | 9 +++++---- routes/protected/containers/Protected.js | 10 +++------- 2 files changed, 8 insertions(+), 11 deletions(-) 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')} />); }; From 6f770595960456f55f59cefa88f609f38f1b5834 Mon Sep 17 00:00:00 2001 From: Marlon del Rosario Date: Fri, 25 Aug 2023 13:46:57 -0400 Subject: [PATCH 4/4] cleanup --- components/Authentication.js | 1 - 1 file changed, 1 deletion(-) diff --git a/components/Authentication.js b/components/Authentication.js index a348c64..4568e0e 100644 --- a/components/Authentication.js +++ b/components/Authentication.js @@ -28,7 +28,6 @@ exports.withAuthentication = function withAuthentication(Component) { prev: route.name } ); - // navigation.navigate('/login'); } else { attemptingRef.current = false;