Skip to content

Commit

Permalink
dynamic initial route for navigation based on auth token existence
Browse files Browse the repository at this point in the history
  • Loading branch information
quincycs committed Mar 4, 2019
1 parent e916476 commit 69f8b76
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
6 changes: 5 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { AppLoading, Asset, Font, Constants } from 'expo';
import { Ionicons } from '@expo/vector-icons';
import Sentry from 'sentry-expo';

import AppNavigator from './navigation/AppNavigator';
import { createAppContainerWithRoute } from './navigation/AppNavigator';
import AppStorage from './utils/AppStorage';

const { extra } = Constants.manifest;
if (extra && extra.sentryEnable) {
Expand Down Expand Up @@ -44,6 +45,7 @@ export default class App extends React.Component<Props, State> {
// to remove this if you are not using it in your app
'space-mono': require('./assets/fonts/SpaceMono-Regular.ttf'),
}),
AppStorage.loadAsync(),
]);
};

Expand Down Expand Up @@ -73,6 +75,8 @@ export default class App extends React.Component<Props, State> {
/>
);
}
const route = AppStorage.getIsLoggedIn() ? 'Main' : 'Auth';
const AppNavigator = createAppContainerWithRoute(route);
return (
<View style={styles.container}>
{Platform.OS === 'ios' && <StatusBar barStyle='default' />}
Expand Down
26 changes: 15 additions & 11 deletions src/navigation/AppNavigator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ import { createAppContainer, createSwitchNavigator } from 'react-navigation';
import MainTabNavigator from './MainTabNavigator';
import AuthNavigator from './AuthNavigator';

export default createAppContainer(
createSwitchNavigator(
{
Auth: AuthNavigator,
Main: MainTabNavigator,
},
{
initialRouteName: 'Auth',
}
)
);
function createAppContainerWithRoute(initialRouteName: string) {
return createAppContainer(
createSwitchNavigator(
{
Auth: AuthNavigator,
Main: MainTabNavigator,
},
{
initialRouteName,
}
)
);
}

export { createAppContainerWithRoute };

0 comments on commit 69f8b76

Please sign in to comment.