|
| 1 | +/** |
| 2 | + * Sample React Native App |
| 3 | + * https://github.com/facebook/react-native |
| 4 | + * |
| 5 | + * @format |
| 6 | + * @flow strict-local |
| 7 | + */ |
| 8 | + |
| 9 | +import React, {useState} from 'react' |
| 10 | +import type {Node} from 'react' |
| 11 | +import {SafeAreaView, StyleSheet, Text, View, Button} from 'react-native' |
| 12 | +import {authorize, logout} from 'react-native-app-auth' |
| 13 | + |
| 14 | +import AppBar from './components/AppBar' |
| 15 | + |
| 16 | +// Get PlusAuth Config |
| 17 | +const config = require('./plusauth-env') |
| 18 | + |
| 19 | +const defaultAuthState = { |
| 20 | + accessToken: '', |
| 21 | + accessTokenExpirationDate: '', |
| 22 | + refreshToken: '', |
| 23 | + idToken: '' |
| 24 | +} |
| 25 | + |
| 26 | +const App: () => Node = () => { |
| 27 | + const [isLoggedIn, setIsLoggedIn] = useState(false) |
| 28 | + const [profileInfo, setProfileInfo] = useState(null) |
| 29 | + const [authState, setAuthState] = useState(defaultAuthState) |
| 30 | + |
| 31 | + const login = async () => { |
| 32 | + try { |
| 33 | + // Redirect PlusAuth Login page to authenticate user |
| 34 | + const result = await authorize(config) |
| 35 | + setAuthState(result) |
| 36 | + setIsLoggedIn(true) |
| 37 | + } catch (error) { |
| 38 | + console.log(error) |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + const signOut = async () => { |
| 43 | + try { |
| 44 | + // Logout session from PlusAuth |
| 45 | + await logout(config, { |
| 46 | + idToken: authState.idToken, |
| 47 | + postLogoutRedirectUrl: config.postLogoutRedirectUrl |
| 48 | + }) |
| 49 | + // Clear local session state |
| 50 | + setAuthState(defaultAuthState) |
| 51 | + setIsLoggedIn(false) |
| 52 | + } catch (error) { |
| 53 | + console.log(error) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + const getProfileInfo = async () => { |
| 58 | + try { |
| 59 | + // Get authenticated user info |
| 60 | + const response = await fetch( |
| 61 | + 'https://starters.plusauth.com/oidc/userinfo', |
| 62 | + { |
| 63 | + method: 'GET', |
| 64 | + headers: { |
| 65 | + Authorization: 'Bearer ' + authState.accessToken |
| 66 | + } |
| 67 | + } |
| 68 | + ) |
| 69 | + const json = await response.json() |
| 70 | + setProfileInfo(json) |
| 71 | + } catch (error) { |
| 72 | + console.error(error) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return ( |
| 77 | + <SafeAreaView> |
| 78 | + <AppBar /> |
| 79 | + <View style={styles.indexContainer}> |
| 80 | + <Text style={styles.welcomeText}> |
| 81 | + Welcome to PlusAuth React Native Demo! |
| 82 | + </Text> |
| 83 | + <Text style={styles.usernameText}> |
| 84 | + Username: {profileInfo === null ? '-' : profileInfo.username} |
| 85 | + </Text> |
| 86 | + <View style={styles.buttonContainer}> |
| 87 | + {!isLoggedIn ? ( |
| 88 | + <Button title="Login" color="#2196F3" onPress={() => login()} /> |
| 89 | + ) : ( |
| 90 | + <> |
| 91 | + <Button |
| 92 | + color="#2196F3" |
| 93 | + title="Get Profile Info" |
| 94 | + onPress={() => getProfileInfo()} |
| 95 | + /> |
| 96 | + <View style={styles.profileButton}> |
| 97 | + <Button |
| 98 | + color="#D32F2F" |
| 99 | + title="Logout" |
| 100 | + onPress={() => signOut()} |
| 101 | + /> |
| 102 | + </View> |
| 103 | + <Text style={{marginTop: 16}}> |
| 104 | + {profileInfo === null ? '' : JSON.stringify(profileInfo)} |
| 105 | + </Text> |
| 106 | + </> |
| 107 | + )} |
| 108 | + </View> |
| 109 | + </View> |
| 110 | + </SafeAreaView> |
| 111 | + ) |
| 112 | +} |
| 113 | + |
| 114 | +const styles = StyleSheet.create({ |
| 115 | + indexContainer: { |
| 116 | + textAlign: 'center' |
| 117 | + }, |
| 118 | + welcomeText: { |
| 119 | + marginTop: 12, |
| 120 | + textAlign: 'center', |
| 121 | + fontSize: 18 |
| 122 | + }, |
| 123 | + usernameText: { |
| 124 | + marginTop: 12, |
| 125 | + textAlign: 'center', |
| 126 | + fontSize: 16 |
| 127 | + }, |
| 128 | + buttonContainer: { |
| 129 | + marginTop: 16, |
| 130 | + paddingHorizontal: 24 |
| 131 | + }, |
| 132 | + profileButton: { |
| 133 | + marginTop: 16 |
| 134 | + } |
| 135 | +}) |
| 136 | + |
| 137 | +export default App |
0 commit comments