123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
-
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <title> src/components/Atlas.tsx</title>
- <script src="https://cdn.jsdelivr.net/gh/google/code-prettify@master/loader/run_prettify.js"></script>
- <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
- <script src="./build/entry.js"></script>
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
- <!--[if lt IE 9]>
- <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
- <![endif]-->
- <link href="https://fonts.googleapis.com/css?family=Roboto:100,400,700|Inconsolata,700" rel="stylesheet">
- <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
- <link type="text/css" rel="stylesheet" href="https://jmblog.github.io/color-themes-for-google-code-prettify/themes/tomorrow-night.min.css">
- <link type="text/css" rel="stylesheet" href="styles/app.min.css">
- <link type="text/css" rel="stylesheet" href="styles/iframe.css">
- <link type="text/css" rel="stylesheet" href="">
- <script async defer src="https://buttons.github.io/buttons.js"></script>
-
- </head>
- <body class="layout small-header">
- <div id="stickyNavbarOverlay"></div>
-
- <div class="top-nav">
- <div class="inner">
- <a id="hamburger" role="button" class="navbar-burger" aria-label="menu" aria-expanded="false">
- <span aria-hidden="true"></span>
- <span aria-hidden="true"></span>
- <span aria-hidden="true"></span>
- </a>
- <div class="logo">
-
-
- </div>
- <div class="menu">
-
- <div class="navigation">
- <a
- href="index.html"
- class="link"
- >
- API Documentation
- </a>
-
-
-
- </div>
- </div>
- </div>
- </div>
- <div id="main">
- <div
- class="sidebar "
- id="sidebarNav"
- >
-
- <nav>
-
- <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>
-
- </nav>
- </div>
- <div class="core" id="main-content-wrapper">
- <div class="content">
- <header class="page-title">
- <p>Source</p>
- <h1>src/components/Atlas.tsx</h1>
- </header>
-
-
- <section>
- <article>
- <pre class="prettyprint source linenums"><code>import {
- RacingSansOne_400Regular
- } from '@expo-google-fonts/racing-sans-one';
- import { NavigationContainer } from '@react-navigation/native';
- import axios, { AxiosRequestConfig } from 'axios';
- import { useFonts } from 'expo-font';
- import { getItemAsync } from 'expo-secure-store';
- import { observer } from 'mobx-react';
- import React, { useEffect, useRef, useState } from 'react';
- import { AppState } from 'react-native';
- import { SafeAreaProvider } from 'react-native-safe-area-context';
- import { QueryClient, QueryClientProvider } from 'react-query';
- import { Splash } from '../components/Splash';
- import { API_URL, reportAxiosError } from '../globals';
- import { useAuth } from '../hooks/useAuth';
- import UnauthorizedNavigator from '../navigation/UnauthorizedNavigator';
- import AuthorizedNavigator from '../navigation/AuthorizedNavigator';
- import { authStore } from '../stores/AuthStore';
- export enum TokenState {
- CheckingToken,
- ValidToken,
- InvalidToken
- }
- const queryClient = new QueryClient();
- /**
- * 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.
- * @component
- */
- const Atlas : React.FC = () => {
- /**
- * Ref that keeps track of the app's state (opened or closed)
- */
- const appState = useRef(AppState.currentState);
- /**
- * 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.
- */
- const [checkingToken, setCheckingToken] = useState<boolean>(true);
- const { refreshAccessToken } = useAuth();
- const [fontsLoaded, error] = useFonts({
- RacingSansOne_400Regular
- });
- /**
- * Checks if there is an access token available in {@link AuthStore}, then checks if that access token is valid by calling the API.
- * If the response is valid, the access token will be stored in memory, otherwise the user will be directed to intro screen.
- */
- const checkToken = async () => {
- // check both the mobx store and secure storage for the token
- let currentAccessToken = authStore.accessToken;
- if (!currentAccessToken) {
- currentAccessToken = await getItemAsync('accessToken');
- }
- if (currentAccessToken) {
- // check to see if the token is valid by making test call
- const requestConfig: AxiosRequestConfig = {
- method: 'GET',
- url: API_URL + "/api/me/",
- headers: { "Authorization": "Bearer " + currentAccessToken }
- };
- try {
- await axios(requestConfig);
- await authStore.setAccessTokenAsync(currentAccessToken);
- } catch (error) {
- // check if access token can be refreshed
- if (error.response.status == 401) {
- try {
- await refreshAccessToken();
- // update authorization header w/ new token
- await axios({...requestConfig, headers: { "Authorization": "Bearer " + authStore.accessToken }});
- } catch (error) {
- await authStore.setAccessTokenAsync(null);
- }
- }
- // something went wrong with the api call, log error and delete access token
- reportAxiosError('Something went wrong when retrieving an access token', error)
- await authStore.setAccessTokenAsync(null);
- }
- }
- else {
- // no access token was found, user will be taken to login
- await authStore.setAccessTokenAsync(null);
- }
- setCheckingToken(false);
- }
- useEffect(() => {
- /**
- * 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.
- * @memberOf Atlas
- */
- function registerAppStateChangeHandler() {
- AppState.addEventListener("change", (appState: string) => {
- if (appState == 'active') {
- checkToken();
- }
- });
- return () => {
- AppState.removeEventListener("change", (appState: string) => {
- if (appState == 'active') {
- checkToken();
- }
- });
- };
- }
- registerAppStateChangeHandler();
- }, []);
- useEffect(() => {
- /**
- * Calls {@linkcode checkToken} when a change to the access token stored in {@link AuthStore} is detected.
- * @memberOf Atlas
- */
- const checkTokenOnAccessTokenChange = () => {
- checkToken()
- }
- checkTokenOnAccessTokenChange()
- }, [authStore.accessToken]);
- return (
- <SafeAreaProvider>
- <NavigationContainer>
- {checkingToken ? <Splash/> :
- <QueryClientProvider client={queryClient}>
- {authStore.accessToken ? <AuthorizedNavigator /> : <UnauthorizedNavigator /> }
- </QueryClientProvider> }
- </NavigationContainer>
- </SafeAreaProvider>
- );
- }
- export default observer(Atlas);</code></pre>
- </article>
- </section>
- </div>
-
- <footer class="footer">
- <div class="content has-text-centered">
- <p>Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.7</a></p>
- <p class="sidebar-created-by">
- <a href="https://github.com/SoftwareBrothers/better-docs" target="_blank">BetterDocs theme</a> provided with <i class="fas fa-heart"></i> by
- <a href="http://softwarebrothers.co" target="_blank">SoftwareBrothers - JavaScript Development Agency</a>
- </p>
- </div>
- </footer>
-
- </div>
- <div id="side-nav" class="side-nav">
- </div>
- </div>
- <script src="scripts/app.min.js"></script>
- <script>PR.prettyPrint();</script>
- <script src="scripts/linenumber.js"> </script>
- </body>
- </html>
|