-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
138 lines (127 loc) · 3.28 KB
/
App.js
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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import React, {useState} from 'react'
import type {Node} from 'react'
import {SafeAreaView, StyleSheet, Text, View, Button} from 'react-native'
import {authorize, logout} from 'react-native-app-auth'
import AppBar from './components/AppBar'
// Get PlusAuth Config
const config = require('./plusauth-env')
const defaultAuthState = {
accessToken: '',
accessTokenExpirationDate: '',
refreshToken: '',
idToken: ''
}
const App: () => Node = () => {
const [isLoggedIn, setIsLoggedIn] = useState(false)
const [profileInfo, setProfileInfo] = useState(null)
const [authState, setAuthState] = useState(defaultAuthState)
const login = async () => {
try {
// Redirect PlusAuth Login page to authenticate user
const result = await authorize(config)
setAuthState(result)
setIsLoggedIn(true)
} catch (error) {
console.log(error)
}
}
const signOut = async () => {
try {
// Logout session from PlusAuth
await logout(config, {
idToken: authState.idToken,
postLogoutRedirectUrl: config.postLogoutRedirectUrl
})
// Clear local session state
setAuthState(defaultAuthState)
setIsLoggedIn(false)
setProfileInfo(null)
} catch (error) {
console.log(error)
}
}
const getProfileInfo = async () => {
try {
// Get authenticated user info
const response = await fetch(
'https://starters.plusauth.com/oidc/userinfo',
{
method: 'GET',
headers: {
Authorization: 'Bearer ' + authState.accessToken
}
}
)
const json = await response.json()
setProfileInfo(json)
} catch (error) {
console.error(error)
}
}
return (
<SafeAreaView>
<AppBar />
<View style={styles.indexContainer}>
<Text style={styles.welcomeText}>
Welcome to PlusAuth React Native Demo!
</Text>
<Text style={styles.usernameText}>
Username: {profileInfo === null ? '-' : profileInfo.username}
</Text>
<View style={styles.buttonContainer}>
{!isLoggedIn ? (
<Button title="Login" color="#2196F3" onPress={() => login()} />
) : (
<>
<Button
color="#2196F3"
title="Get Profile Info"
onPress={() => getProfileInfo()}
/>
<View style={styles.profileButton}>
<Button
color="#D32F2F"
title="Logout"
onPress={() => signOut()}
/>
</View>
<Text style={{marginTop: 16}}>
{profileInfo === null ? '' : JSON.stringify(profileInfo)}
</Text>
</>
)}
</View>
</View>
</SafeAreaView>
)
}
const styles = StyleSheet.create({
indexContainer: {
textAlign: 'center'
},
welcomeText: {
marginTop: 12,
textAlign: 'center',
fontSize: 18
},
usernameText: {
marginTop: 12,
textAlign: 'center',
fontSize: 16
},
buttonContainer: {
marginTop: 16,
paddingHorizontal: 24
},
profileButton: {
marginTop: 16
}
})
export default App