src_components_Auth_Intro.tsx.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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/Auth/Intro.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/Auth/Intro.tsx</h1>
  59. </header>
  60. <section>
  61. <article>
  62. <pre class="prettyprint source linenums"><code>import { Link } from "@react-navigation/native";
  63. import { StackNavigationProp } from "@react-navigation/stack";
  64. import axios from "axios";
  65. import { loadAsync, makeRedirectUri, ResponseType } from "expo-auth-session";
  66. import { maybeCompleteAuthSession } from "expo-web-browser";
  67. import jwt_decode from 'jwt-decode';
  68. import React, { useState } from "react";
  69. import { ActivityIndicator, Image, StyleSheet, Text, TouchableOpacity, View } from "react-native";
  70. import { API_URL, reportAxiosError } from "../../globals";
  71. import { AuthStackNavigationProp } from "../../navigation/UnauthorizedNavigator";
  72. import { authStore } from "../../stores/AuthStore";
  73. import UnauthorizedLayout from "./AuthLayout";
  74. import { PrimaryButton, SecondaryButton } from "./Buttons";
  75. import { IdToken } from "../../stores/AuthStore";
  76. import * as WebBrowser from 'expo-web-browser';
  77. /**
  78. * Props used by the {@link Intro} screen.
  79. */
  80. export interface IntroProps {
  81. /**The {@link AuthStackNavigationProp} navigation object used to interact with the {@link Auth} navigator.*/
  82. navigation: AuthStackNavigationProp;
  83. }
  84. const issuer = API_URL + "/o";
  85. const discovery = {
  86. authorizationEndpoint: issuer + "/authorize/",
  87. tokenEndpoint: issuer + "/token/",
  88. revocationEndpoint: issuer + "/revoke/",
  89. };
  90. maybeCompleteAuthSession();
  91. /**
  92. * The Intro screen displayed to unauthenticated users. Contains necessary methods to facilitate login and navigation to the [Registration]{@link RegisterMain} screens.
  93. * @component
  94. * @category Unauthorized
  95. */
  96. const Intro : React.FC&lt;IntroProps> = ({navigation}) => {
  97. /**
  98. * @type {string}
  99. * React state holding the login result message to display to the user.
  100. * */
  101. const loginMessageState = "";
  102. const [loginMessage, setLoginMessage] = useState&lt;string>(loginMessageState);
  103. /**
  104. * @type {boolean}
  105. * React state holding the error state of the component.
  106. * */
  107. const errorState = false;
  108. const [error, setError] = useState&lt;boolean>(errorState);
  109. const redirectUri = makeRedirectUri({
  110. path: 'callback'
  111. });
  112. /**
  113. * Function that initiates the login flow. It opens up with the in app browser and sends an authorization request to the API, which redirects the user to the backend login page.
  114. * If the credentials entered are valid, the OAuth Authorization Code flow will occur, which results in the user recieving an access token and refresh token which are then stored in {@link AuthStore}.
  115. * */
  116. const login = async (formValues: any) => {
  117. const request = await loadAsync({
  118. clientId: "atlas.mobile",
  119. responseType: ResponseType.Code,
  120. redirectUri,
  121. usePKCE: true,
  122. scopes: ['openid'],
  123. }, discovery)
  124. const response = await request.promptAsync(discovery);
  125. if (response.type == "success" &amp;&amp; request.codeVerifier) {
  126. setLoginMessage("Logging you in...");
  127. const tokenData = new URLSearchParams();
  128. tokenData.append('grant_type', 'authorization_code');
  129. tokenData.append('client_id', 'atlas.mobile');
  130. tokenData.append('code', response.params.code);
  131. tokenData.append('redirect_uri', request.redirectUri);
  132. tokenData.append('code_verifier', request.codeVerifier);
  133. try {
  134. const response = await axios.post(API_URL + `/o/token/`, tokenData, {
  135. headers: {
  136. 'Content-Type': "application/x-www-form-urlencoded"
  137. },
  138. });
  139. const tokenResponse = response.data;
  140. const idToken = jwt_decode(tokenResponse.id_token) as IdToken;
  141. const accessToken = tokenResponse.access_token;
  142. const refreshToken = tokenResponse.refresh_token;
  143. authStore.setAccessTokenAsync(accessToken);
  144. authStore.setRefreshTokenAsync(refreshToken);
  145. authStore.setIdAsync(idToken.sub)
  146. } catch (error) {
  147. reportAxiosError("Something went wrong when retrieving access token", error);
  148. console.log(error)
  149. setLoginMessage("Something went wrong when trying to log you in.")
  150. setError(true);
  151. }
  152. }
  153. }
  154. /**
  155. * Navigates to {@link RegisterMain}.
  156. */
  157. const goToRegistration = (): void => {
  158. navigation.navigate("Register");
  159. }
  160. /**
  161. * Opens up the [privacy policy page]{@link https://app.clicknpush.ca/privacy}.
  162. */
  163. const openPrivacyPolicy = async () => {
  164. await WebBrowser.openBrowserAsync(API_URL + "/privacy")
  165. }
  166. return (
  167. &lt;UnauthorizedLayout>
  168. {!loginMessage ?
  169. &lt;View style={styles.introContainer}>
  170. &lt;View style={styles.brandContainer}>
  171. &lt;Image style={{flex: 1}} resizeMode="contain" source={require('../../../assets/logo-white.png')}>&lt;/Image>
  172. &lt;Text style={styles.title} >Click &amp; Push&lt;/Text>
  173. &lt;/View>
  174. &lt;View style={styles.btnContainer}>
  175. &lt;PrimaryButton text="Login" onPress={login}/>
  176. &lt;SecondaryButton text="Create account" onPress={goToRegistration} />
  177. &lt;/View>
  178. &lt;/View> :
  179. &lt;View style={{height: '100%', justifyContent: "center", alignItems: "center"}}>
  180. &lt;Text style={{color: 'white', fontSize: 20, marginBottom: 30}}>{loginMessage}&lt;/Text>
  181. {!error ? &lt;ActivityIndicator color='white' size="large"/> : &lt;PrimaryButton text="Okay" onPress={() => {setError(false); setLoginMessage('')}}/> }
  182. &lt;/View> }
  183. &lt;TouchableOpacity onPress={openPrivacyPolicy}>
  184. &lt;Text style={{fontSize: 12, textDecorationLine: "underline", alignSelf: "flex-end", color: 'white'}}>Privacy policy&lt;/Text>
  185. &lt;/TouchableOpacity>
  186. &lt;/UnauthorizedLayout>
  187. )
  188. }
  189. const styles = StyleSheet.create({
  190. introContainer: {
  191. flex: 1,
  192. marginVertical: 50,
  193. alignItems: "center",
  194. justifyContent: 'space-between'
  195. },
  196. brandContainer: {
  197. flex: 2,
  198. marginVertical: 50,
  199. alignItems: "center",
  200. },
  201. title: {
  202. marginTop: 30,
  203. color: 'white',
  204. fontSize: 30
  205. },
  206. btnContainer: {
  207. flex: 1,
  208. alignItems: 'center',
  209. width: '100%',
  210. },
  211. registerBtn: {
  212. borderColor: 'white',
  213. borderWidth: 2,
  214. backgroundColor: 'transparent',
  215. }
  216. })
  217. export default Intro;</code></pre>
  218. </article>
  219. </section>
  220. </div>
  221. <footer class="footer">
  222. <div class="content has-text-centered">
  223. <p>Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.6.7</a></p>
  224. <p class="sidebar-created-by">
  225. <a href="https://github.com/SoftwareBrothers/better-docs" target="_blank">BetterDocs theme</a> provided with <i class="fas fa-heart"></i> by
  226. <a href="http://softwarebrothers.co" target="_blank">SoftwareBrothers - JavaScript Development Agency</a>
  227. </p>
  228. </div>
  229. </footer>
  230. </div>
  231. <div id="side-nav" class="side-nav">
  232. </div>
  233. </div>
  234. <script src="scripts/app.min.js"></script>
  235. <script>PR.prettyPrint();</script>
  236. <script src="scripts/linenumber.js"> </script>
  237. </body>
  238. </html>