-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.js
93 lines (87 loc) · 2.32 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
import * as React from 'react'
import { Button, Text, View } from 'react-native'
import jwtDecode from "jwt-decode";
import {
useAuthRequest,
exchangeCodeAsync,
revokeAsync,
} from 'expo-auth-session'
import * as WebBrowser from 'expo-web-browser'
import { clientId, redirectUri, discovery } from './constant'
WebBrowser.maybeCompleteAuthSession()
export default function App() {
const [authTokens, setAuthTokens] = React.useState(null)
const [request, response, promptAsync] = useAuthRequest(
{
clientId,
scopes: ['openid', 'offline', 'bookmark'],
redirectUri,
usePKCE: true,
},
discovery
)
React.useEffect(() => {
const exchangeFn = async (exchangeTokenReq) => {
try {
const exchangeTokenResponse = await exchangeCodeAsync(
exchangeTokenReq,
discovery
)
const { idToken } = exchangeTokenResponse;
const decoded = jwtDecode(idToken);
setAuthTokens({ ...exchangeTokenResponse, userProfile: decoded });
} catch (error) {
console.error(error)
}
}
if (response) {
if (response.error) {
Alert.alert(
'Authentication error',
response.params.error_description || 'something went wrong'
)
return
}
if (response.type === 'success') {
exchangeFn({
clientId,
code: response.params.code,
redirectUri,
extraParams: {
code_verifier: request.codeVerifier,
},
})
}
}
}, [discovery, request, response])
const logout = async () => {
console.log({ authTokens })
const revokeResponse = await revokeAsync(
{
clientId: clientId,
token: authTokens.refreshToken,
},
discovery
)
if (revokeResponse) {
setAuthTokens(null)
}
}
console.log('authTokens: ' + JSON.stringify(authTokens))
if (authTokens) {
const { userProfile: { first_name, last_name } } = authTokens
return <View style={{ paddingTop: 200 }}>
<Text>Welcome {`${first_name} ${last_name}`}!</Text>
<Button title="Logout" onPress={() => logout()} />
</View>
}
return (
<View style={{ paddingTop: 200 }}>
<Button
disabled={!request}
title="Login"
onPress={() => promptAsync()}
/>
</View>
)
}