-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
131 lines (125 loc) · 4.23 KB
/
App.js
File metadata and controls
131 lines (125 loc) · 4.23 KB
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
/**
* Identicum OIDC React Native App
* @author mbesozzi
* @format
* @flow strict-local
*/
import React, { Component} from 'react';
import { Button, StyleSheet, View, Text, ScrollView, SafeAreaView} from 'react-native';
import { authorize } from 'react-native-app-auth';
import { Buffer } from 'buffer';
const configs = {
provider : {
issuer: '{idpIssuer}',
clientId: '{clientId}',
redirectUrl: 'com.identicum.mobile.demo:/oauthredirect',
additionalParameters: {},
scopes: ['openid', 'profile'],
serviceConfiguration: {
authorizationEndpoint: '{idpAauthorizationEndpoint}',
tokenEndpoint: '{idpTokenEndpoint}',
revocationEndpoint: '{idpRevocationEndpoint}'
}
}
};
class App extends Component {
constructor(props) {
super(props);
this.state = { accessToken: '', idToken : '' };
}
decodeJwt = (idToken) => {
console.log("____ Decoding ID token: " + this.state.idToken)
const jwtBody = idToken.split('.')[1];
const base64 = jwtBody.replace('-', '+').replace('_', '/');
const decodedJwt = Buffer.from(base64, 'base64');
console.log("____ Decoded ID token: " + decodedJwt)
return JSON.parse(decodedJwt);
}
_onLogin = () => {
console.log("___ Login...")
authorize(configs.provider).then(authResponse => {
console.log("___ Response:" + authResponse);
this.setState( {
accessToken: authResponse.accessToken ,
idToken : authResponse.idToken
});
})
.catch(error => console.log(error));
};
_onLogout = () => {
console.log("___ Logout...")
// TODO: Handling revoke token
this.setState({
accessToken: '',
idToken : ''
});
};
render() {
let loggedIn = this.state.accessToken == '' ? false : true;
let decodedJwt;
if(loggedIn)
decodedJwt = this.decodeJwt(this.state.idToken);
console.log("___ Is User logged in : " + loggedIn );
return (
<View style={styles.container}>
<View>
<Text style={styles.headerText}>{ loggedIn ? "User: " + decodedJwt.sub : "Welcome" } </Text>
</View>
<View style={styles.content}>
{ loggedIn ? (
<ScrollView contentContainerStyle={{ paddingTop: 10, flexGrow: 1, justifyContent: 'center' , width: "100%"}}>
<View style = {[{ width: "90%" , marginBottom : 50}]}>
<View >
<Text style={styles.fieldHeader}>Access Token</Text>
<Text style={styles.fieldValue} selectable>{this.state.accessToken}</Text>
<Text style={styles.fieldHeader}>ID Token </Text>
<Text style={styles.fieldValue} selectable> {JSON.stringify(decodedJwt)}</Text>
<Text style={styles.fieldHeader}>ID Token (JWT)</Text>
<Text style={styles.fieldValue} selectable>{this.state.idToken}</Text>
</View>
</View>
</ScrollView>
): <View></View>}
<View style={ loggedIn ? styles.bottomView : [{ width: "90%"}] } >
<Button onPress = { loggedIn ? this._onLogout : this._onLogin } title = { loggedIn ? 'Log Out' : 'Log In' } />
</View>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#F5FCFF',
},
content: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
headerText: {
fontSize: 30,
fontWeight: 'bold',
color: '#333',
textAlign: 'center',
marginTop: 35
},
fieldHeader : {
alignSelf:'center',
fontSize: 20,
justifyContent:'center',
alignItems:'center'
},
bottomView:{
width: '90%',
position: 'absolute',
bottom: 20
},
fieldValue : {
color: '#0071BC',
fontSize: 15,
margin: 20
}
});
export default App;