Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Logout wiring #17

Open
wants to merge 4 commits into
base: create-protected-route
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions components/Authentication.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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 (<LoadingOverlay />);
Expand Down
10 changes: 9 additions & 1 deletion routes/auth/containers/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
};

Expand Down
9 changes: 5 additions & 4 deletions routes/protected/components/Protected.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,24 @@ 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;

return (
<Wrapper {...props}>
<View>
<BigText>This page is exclusive to logged-in users.</BigText>
<CenteredButton onPress={() => navigation.navigate('/demo')}>Go to Demo</CenteredButton>
<CenteredButton appearance='ghost' onPress={() => navigation.navigate('/login')}>Log Out</CenteredButton>
<CenteredButton onPress={onPressDemo}>Go to Demo</CenteredButton>
<CenteredButton appearance='ghost' onPress={onPressLogout}>Log Out</CenteredButton>
</View>
</Wrapper>
);
};

module.exports.propTypes = {
navigation: T.object.isRequired
onPressLogout: T.func.isRequired,
onPressDemo: T.func.isRequired
};

internals.BigText = Styled(Text)`
Expand Down
10 changes: 3 additions & 7 deletions routes/protected/containers/Protected.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (<Protected {...props} onPressLogout={onPressLogout} />);
return (<Protected {...props} onPressLogout={onPressLogout} onPressDemo={() => navigation.navigate('/demo')} />);
};