MainStackNavigator.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import 'react-native-gesture-handler';
  2. import React, { useEffect, useState } from 'react';
  3. import { createStackNavigator } from '@react-navigation/stack';
  4. import { DefaultTheme } from 'react-native-paper'
  5. import Intro from '../screens/Intro';
  6. import { RegistrationFormProvider } from '../contexts/RegisterContext';
  7. import { MapProvider } from '../contexts/MapContext';
  8. import TabsNavigator from './TabsNavigator';
  9. import { useAuthState } from '../contexts/AuthContext';
  10. import RegisterMain from '../screens/RegisterMain';
  11. import Splash from '../screens/Splash';
  12. import * as SecureStore from 'expo-secure-store';
  13. import axios from 'axios';
  14. const MainStack = createStackNavigator();
  15. const theme = {
  16. ...DefaultTheme,
  17. colors: {
  18. primary: '#df3f3f',
  19. accent: 'white',
  20. },
  21. }
  22. const MainStackNavigator = () => {
  23. const {authDispatch, authState} = useAuthState();
  24. useEffect(() => {
  25. const checkToken = async () => {
  26. let accessToken;
  27. try {
  28. accessToken = await SecureStore.getItemAsync('accessToken');
  29. if (accessToken) {
  30. try {
  31. // send a test request to check if access token valid
  32. const response = await axios.get(`http://10.0.0.151:8000/api/me/`, {
  33. headers: {
  34. 'Authorization': "Bearer " + accessToken
  35. }
  36. });
  37. if (response.status === 200) {
  38. // response token is valid, set it in state so user is directed to the map
  39. authDispatch({type: "SET_ACCESS_TOKEN", payload: accessToken});
  40. }
  41. }
  42. catch (error) {
  43. // initial access token is invalid, user has to login
  44. accessToken = await SecureStore.deleteItemAsync('accessToken');
  45. authDispatch({type: "SET_ACCESS_TOKEN", payload: null});
  46. // other errors funnel here so log them
  47. console.log(error);
  48. console.log(error.response.data);
  49. }
  50. }
  51. else {
  52. // access token doesnt exist, user will be directed to the intro screen
  53. authDispatch({type: "SET_ACCESS_TOKEN", payload: null});
  54. }
  55. }
  56. catch (err) {
  57. console.log(err);
  58. }
  59. }
  60. checkToken();
  61. }, [authState.accessToken])
  62. return(
  63. <MapProvider><RegistrationFormProvider>
  64. <MainStack.Navigator screenOptions={{
  65. headerTransparent: true,
  66. headerTintColor: 'white',
  67. headerTitle: ""
  68. }}>
  69. {authState.isStarting ?
  70. <MainStack.Screen name="Splash" component={Splash} /> :
  71. <>
  72. {authState.accessToken ?
  73. <MainStack.Screen name="MainTabs" component={TabsNavigator} /> :
  74. <>
  75. <MainStack.Screen name="Intro" component={Intro} />
  76. <MainStack.Screen name="Registration" component={RegisterMain}/>
  77. </>
  78. }
  79. </>}
  80. </MainStack.Navigator>
  81. </RegistrationFormProvider></MapProvider>
  82. )
  83. }
  84. export default MainStackNavigator;