src_components_Atlas.tsx.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <title> src/components/Atlas.tsx</title>
  7. <script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js"></script>
  8. <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
  9. <script src="./build/entry.js"></script>
  10. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  11. <!--[if lt IE 9]>
  12. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  13. <![endif]-->
  14. <link href="https://fonts.googleapis.com/css?family=Roboto:100,400,700|Inconsolata,700" rel="stylesheet">
  15. <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
  16. <link type="text/css" rel="stylesheet" href="https://jmblog.github.io/color-themes-for-google-code-prettify/themes/tomorrow-night.min.css">
  17. <link type="text/css" rel="stylesheet" href="styles/app.min.css">
  18. <link type="text/css" rel="stylesheet" href="styles/iframe.css">
  19. <link type="text/css" rel="stylesheet" href="">
  20. <script async defer src="https://buttons.github.io/buttons.js"></script>
  21. </head>
  22. <body class="layout small-header">
  23. <div id="stickyNavbarOverlay"></div>
  24. <div class="top-nav">
  25. <div class="inner">
  26. <a id="hamburger" role="button" class="navbar-burger" aria-label="menu" aria-expanded="false">
  27. <span aria-hidden="true"></span>
  28. <span aria-hidden="true"></span>
  29. <span aria-hidden="true"></span>
  30. </a>
  31. <div class="logo">
  32. </div>
  33. <div class="menu">
  34. <div class="navigation">
  35. <a
  36. href="index.html"
  37. class="link"
  38. >
  39. API Documentation
  40. </a>
  41. </div>
  42. </div>
  43. </div>
  44. </div>
  45. <div id="main">
  46. <div
  47. class="sidebar "
  48. id="sidebarNav"
  49. >
  50. <nav>
  51. <h2><a href="index.html">Documentation</a></h2><div class="category"><h3>Interfaces</h3><ul><li><a href="AddLandmarkProps.html">AddLandmarkProps</a></li><li><a href="CommentProps.html">CommentProps</a></li><li><a href="IdToken.html">IdToken</a></li><li><a href="IntroProps.html">IntroProps</a></li><li><a href="Landmark.html">Landmark</a></li><li><a href="LandmarkDetailsProps.html">LandmarkDetailsProps</a></li><li><a href="LandmarkPinProps.html">LandmarkPinProps</a></li><li><a href="LMComment.html">LMComment</a></li><li><a href="ProfileProps.html">ProfileProps</a></li><li><a href="RegisterProps.html">RegisterProps</a></li><li><a href="UserProfile.html">UserProfile</a></li></ul><h3>Components</h3><ul><li><a href="App.html">App</a></li><li><a href="Atlas.html">Atlas</a></li><li><a href="Comment.html">Comment</a></li><li><a href="PrimaryButton.html">PrimaryButton</a></li><li><a href="Profile.html">Profile</a></li><li><a href="SecondaryButton.html">SecondaryButton</a></li></ul><h3>Global</h3><ul><li><a href="global.html#IconStrings">IconStrings</a></li></ul></div><div class="category"><h2>Hooks</h2><h3>Namespaces</h3><ul><li><a href="useAuth.html">useAuth</a></li><li><a href="useLandmarks.html">useLandmarks</a></li><li><a href="useProfile.html">useProfile</a></li></ul></div><div class="category"><h2>Map</h2><h3>Interfaces</h3><ul><li><a href="UserLocation.html">UserLocation</a></li></ul><h3>Components</h3><ul><li><a href="AddLandmark.html">AddLandmark</a></li><li><a href="LandmarkDetails.html">LandmarkDetails</a></li><li><a href="LandmarkPin.html">LandmarkPin</a></li><li><a href="Map.html">Map</a></li></ul></div><div class="category"><h2>Navigation</h2><h3>Components</h3><ul><li><a href="AuthorizedNavigator.html">AuthorizedNavigator</a></li><li><a href="UnauthorizedNavigator.html">UnauthorizedNavigator</a></li></ul><h3><a href="global.html">Global</a></h3></div><div class="category"><h2>Stores</h2><h3>Classes</h3><ul><li><a href="AuthStore.html">AuthStore</a></li></ul></div><div class="category"><h2>Unauthorized</h2><h3>Components</h3><ul><li><a href="Intro.html">Intro</a></li><li><a href="UnauthorizedLayout.html">UnauthorizedLayout</a></li></ul><h3>Components / Registration</h3><ul><li><a href="RegisterMain.html">RegisterMain</a></li></ul></div>
  52. </nav>
  53. </div>
  54. <div class="core" id="main-content-wrapper">
  55. <div class="content">
  56. <header class="page-title">
  57. <p>Source</p>
  58. <h1>src/components/Atlas.tsx</h1>
  59. </header>
  60. <section>
  61. <article>
  62. <pre class="prettyprint source linenums"><code>import {
  63. RacingSansOne_400Regular
  64. } from '@expo-google-fonts/racing-sans-one';
  65. import { NavigationContainer } from '@react-navigation/native';
  66. import axios, { AxiosRequestConfig } from 'axios';
  67. import { useFonts } from 'expo-font';
  68. import { getItemAsync } from 'expo-secure-store';
  69. import { observer } from 'mobx-react';
  70. import React, { useEffect, useRef, useState } from 'react';
  71. import { AppState } from 'react-native';
  72. import { SafeAreaProvider } from 'react-native-safe-area-context';
  73. import { QueryClient, QueryClientProvider } from 'react-query';
  74. import { Splash } from '../components/Splash';
  75. import { API_URL, reportAxiosError } from '../globals';
  76. import { useAuth } from '../hooks/useAuth';
  77. import UnauthorizedNavigator from '../navigation/UnauthorizedNavigator';
  78. import AuthorizedNavigator from '../navigation/AuthorizedNavigator';
  79. import { authStore } from '../stores/AuthStore';
  80. export enum TokenState {
  81. CheckingToken,
  82. ValidToken,
  83. InvalidToken
  84. }
  85. const queryClient = new QueryClient();
  86. /**
  87. * Sub-root component of the app. Contains all global providers (NavigationContainer and SafeAreaProvider for React Navigation, QueryClientProvider for react-query) and is responsible for restricting unauthenticated users to the Intro screen by listening to {@link AuthStore}'s accessToken value.
  88. * @component
  89. */
  90. const Atlas : React.FC = () => {
  91. /**
  92. * Ref that keeps track of the app's state (opened or closed)
  93. */
  94. const appState = useRef(AppState.currentState);
  95. /**
  96. * Flag that is switched on when the app is checking for tokens in the keystore and in memory. When true, "Logging you in.." and a spinner will be displayed to the user.
  97. */
  98. const [checkingToken, setCheckingToken] = useState&lt;boolean>(true);
  99. const { refreshAccessToken } = useAuth();
  100. const [fontsLoaded, error] = useFonts({
  101. RacingSansOne_400Regular
  102. });
  103. /**
  104. * Checks if there is an access token available in {@link AuthStore}, then checks if that access token is valid by calling the API.
  105. * If the response is valid, the access token will be stored in memory, otherwise the user will be directed to intro screen.
  106. */
  107. const checkToken = async () => {
  108. // check both the mobx store and secure storage for the token
  109. let currentAccessToken = authStore.accessToken;
  110. if (!currentAccessToken) {
  111. currentAccessToken = await getItemAsync('accessToken');
  112. }
  113. if (currentAccessToken) {
  114. // check to see if the token is valid by making test call
  115. const requestConfig: AxiosRequestConfig = {
  116. method: 'GET',
  117. url: API_URL + "/api/me/",
  118. headers: { "Authorization": "Bearer " + currentAccessToken }
  119. };
  120. try {
  121. await axios(requestConfig);
  122. await authStore.setAccessTokenAsync(currentAccessToken);
  123. } catch (error) {
  124. // check if access token can be refreshed
  125. if (error.response.status == 401) {
  126. try {
  127. await refreshAccessToken();
  128. // update authorization header w/ new token
  129. await axios({...requestConfig, headers: { "Authorization": "Bearer " + authStore.accessToken }});
  130. } catch (error) {
  131. await authStore.setAccessTokenAsync(null);
  132. }
  133. }
  134. // something went wrong with the api call, log error and delete access token
  135. reportAxiosError('Something went wrong when retrieving an access token', error)
  136. await authStore.setAccessTokenAsync(null);
  137. }
  138. }
  139. else {
  140. // no access token was found, user will be taken to login
  141. await authStore.setAccessTokenAsync(null);
  142. }
  143. setCheckingToken(false);
  144. }
  145. useEffect(() => {
  146. /**
  147. * useEffect hook that is responsible for registering an appState "change" handler that will call {@linkcode checkToken} each time the app is opened or closed on the device.
  148. * @memberOf Atlas
  149. */
  150. function registerAppStateChangeHandler() {
  151. AppState.addEventListener("change", (appState: string) => {
  152. if (appState == 'active') {
  153. checkToken();
  154. }
  155. });
  156. return () => {
  157. AppState.removeEventListener("change", (appState: string) => {
  158. if (appState == 'active') {
  159. checkToken();
  160. }
  161. });
  162. };
  163. }
  164. registerAppStateChangeHandler();
  165. }, []);
  166. useEffect(() => {
  167. /**
  168. * Calls {@linkcode checkToken} when a change to the access token stored in {@link AuthStore} is detected.
  169. * @memberOf Atlas
  170. */
  171. const checkTokenOnAccessTokenChange = () => {
  172. checkToken()
  173. }
  174. checkTokenOnAccessTokenChange()
  175. }, [authStore.accessToken]);
  176. return (
  177. &lt;SafeAreaProvider>
  178. &lt;NavigationContainer>
  179. {checkingToken ? &lt;Splash/> :
  180. &lt;QueryClientProvider client={queryClient}>
  181. {authStore.accessToken ? &lt;AuthorizedNavigator /> : &lt;UnauthorizedNavigator /> }
  182. &lt;/QueryClientProvider> }
  183. &lt;/NavigationContainer>
  184. &lt;/SafeAreaProvider>
  185. );
  186. }
  187. export default observer(Atlas);</code></pre>
  188. </article>
  189. </section>
  190. </div>
  191. <footer class="footer">
  192. <div class="content has-text-centered">
  193. <p>Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.7</a></p>
  194. <p class="sidebar-created-by">
  195. <a href="https://github.com/SoftwareBrothers/better-docs" target="_blank">BetterDocs theme</a> provided with <i class="fas fa-heart"></i> by
  196. <a href="http://softwarebrothers.co" target="_blank">SoftwareBrothers - JavaScript Development Agency</a>
  197. </p>
  198. </div>
  199. </footer>
  200. </div>
  201. <div id="side-nav" class="side-nav">
  202. </div>
  203. </div>
  204. <script src="scripts/app.min.js"></script>
  205. <script>PR.prettyPrint();</script>
  206. <script src="scripts/linenumber.js"> </script>
  207. </body>
  208. </html>