Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x | import { Link } from "@react-navigation/native"; import { StackNavigationProp } from "@react-navigation/stack"; import axios from "axios"; import { loadAsync, makeRedirectUri, ResponseType } from "expo-auth-session"; import { maybeCompleteAuthSession } from "expo-web-browser"; import jwt_decode from 'jwt-decode'; import React, { useState } from "react"; import { ActivityIndicator, Image, StyleSheet, Text, TouchableOpacity, View } from "react-native"; import { API_URL, reportAxiosError } from "../../globals"; import { AuthStackNavigationProp } from "../../navigation/UnauthorizedNavigator"; import { authStore } from "../../stores/AuthStore"; import UnauthorizedLayout from "./AuthLayout"; import { PrimaryButton, SecondaryButton } from "./Buttons"; import { IdToken } from "../../stores/AuthStore"; import * as WebBrowser from 'expo-web-browser'; /** * Props used by the {@link Intro} screen. */ export interface IntroProps { /**The {@link AuthStackNavigationProp} navigation object used to interact with the {@link Auth} navigator.*/ navigation: AuthStackNavigationProp; } const issuer = API_URL + "/o"; const discovery = { authorizationEndpoint: issuer + "/authorize/", tokenEndpoint: issuer + "/token/", revocationEndpoint: issuer + "/revoke/", }; maybeCompleteAuthSession(); /** * The Intro screen displayed to unauthenticated users. Contains necessary methods to facilitate login and navigation to the [Registration]{@link RegisterMain} screens. * @component * @category Unauthorized */ const Intro : React.FC<IntroProps> = ({navigation}) => { /** * @type {string} * React state holding the login result message to display to the user. * */ const loginMessageState = ""; const [loginMessage, setLoginMessage] = useState<string>(loginMessageState); /** * @type {boolean} * React state holding the error state of the component. * */ const errorState = false; const [error, setError] = useState<boolean>(errorState); const redirectUri = makeRedirectUri({ path: 'callback' }); /** * 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. * 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}. * */ const login = async (formValues: any) => { const request = await loadAsync({ clientId: "atlas.mobile", responseType: ResponseType.Code, redirectUri, usePKCE: true, scopes: ['openid'], }, discovery) const response = await request.promptAsync(discovery); if (response.type == "success" && request.codeVerifier) { setLoginMessage("Logging you in..."); const tokenData = new URLSearchParams(); tokenData.append('grant_type', 'authorization_code'); tokenData.append('client_id', 'atlas.mobile'); tokenData.append('code', response.params.code); tokenData.append('redirect_uri', request.redirectUri); tokenData.append('code_verifier', request.codeVerifier); try { const response = await axios.post(API_URL + `/o/token/`, tokenData, { headers: { 'Content-Type': "application/x-www-form-urlencoded" }, }); const tokenResponse = response.data; const idToken = jwt_decode(tokenResponse.id_token) as IdToken; const accessToken = tokenResponse.access_token; const refreshToken = tokenResponse.refresh_token; authStore.setAccessTokenAsync(accessToken); authStore.setRefreshTokenAsync(refreshToken); authStore.setIdAsync(idToken.sub) } catch (error) { reportAxiosError("Something went wrong when retrieving access token", error); console.log(error) setLoginMessage("Something went wrong when trying to log you in.") setError(true); } } } /** * Navigates to {@link RegisterMain}. */ const goToRegistration = (): void => { navigation.navigate("Register"); } /** * Opens up the [privacy policy page]{@link https://app.clicknpush.ca/privacy}. */ const openPrivacyPolicy = async () => { await WebBrowser.openBrowserAsync(API_URL + "/privacy") } return ( <UnauthorizedLayout> {!loginMessage ? <View style={styles.introContainer}> <View style={styles.brandContainer}> <Image style={{flex: 1}} resizeMode="contain" source={require('../../../assets/logo-white.png')}></Image> <Text style={styles.title} >Click & Push</Text> </View> <View style={styles.btnContainer}> <PrimaryButton text="Login" onPress={login}/> <SecondaryButton text="Create account" onPress={goToRegistration} /> </View> </View> : <View style={{height: '100%', justifyContent: "center", alignItems: "center"}}> <Text style={{color: 'white', fontSize: 20, marginBottom: 30}}>{loginMessage}</Text> {!error ? <ActivityIndicator color='white' size="large"/> : <PrimaryButton text="Okay" onPress={() => {setError(false); setLoginMessage('')}}/> } </View> } <TouchableOpacity onPress={openPrivacyPolicy}> <Text style={{fontSize: 12, textDecorationLine: "underline", alignSelf: "flex-end", color: 'white'}}>Privacy policy</Text> </TouchableOpacity> </UnauthorizedLayout> ) } const styles = StyleSheet.create({ introContainer: { flex: 1, marginVertical: 50, alignItems: "center", justifyContent: 'space-between' }, brandContainer: { flex: 2, marginVertical: 50, alignItems: "center", }, title: { marginTop: 30, color: 'white', fontSize: 30 }, btnContainer: { flex: 1, alignItems: 'center', width: '100%', }, registerBtn: { borderColor: 'white', borderWidth: 2, backgroundColor: 'transparent', } }) export default Intro; |