-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHome.js
More file actions
executable file
·194 lines (190 loc) · 5.16 KB
/
Copy pathHome.js
File metadata and controls
executable file
·194 lines (190 loc) · 5.16 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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* react-native-aad-b2c-sample
* https://github.com/michaelburch/react-native-aad-b2c-sample
*
* This component retrieves an access token from adService
* and displays it.
*/
import {Text, StyleSheet, View, TouchableOpacity} from 'react-native';
import React, {Component} from 'react';
import {adService} from 'ad-b2c-react-native';
import jwt_decode from 'jwt-decode';
export default class Home extends Component {
constructor(props) {
super();
this.state = {
token: '',
expires: '',
iss: '',
aud: '',
idp: '',
emails: [''],
name: '',
subject: '',
};
}
// Navigate to the Logout screen when Logout button is pressed
onLogout = () => {
const {navigation} = this.props;
navigation.navigate('Logout');
};
// Retrieve the access token
getToken = async () => {
let token = await adService.getAccessTokenAsync();
let date = '';
this.setState({token: token.data.split(' ')[1]});
// Attempt to decode the token
try {
let decoded_token = jwt_decode(token.data.split(' ')[1]);
try {
date = new Date(decoded_token.exp * 1000);
date = date.toString();
} catch {}
// Update state with decoded token values
this.setState({
token: decoded_token,
expires: date,
idp: decoded_token.idp,
iss: decoded_token.iss,
aud: decoded_token.aud,
sub: decoded_token.sub,
name: decoded_token.name,
emails: decoded_token.emails,
});
} catch {}
};
componentDidMount = () => {
this.getToken();
};
render() {
return (
// Define the view
<View style={styles.body}>
<View style={styles.frame} />
<Text style={styles.headerText}>Access Token Details</Text>
<View style={styles.body}>
{/* Identity Provider */}
<View style={styles.row}>
<View style={styles.label}>
<Text style={[styles.labelText]}>Identity provider:</Text>
</View>
<View style={styles.value}>
<Text style={styles.valueText}>{this.state.idp}</Text>
</View>
</View>
{/* Token Expiration */}
<View style={styles.row}>
<View style={styles.label}>
<Text style={[styles.labelText]}>Expiration:</Text>
</View>
<View style={styles.value}>
<Text style={styles.valueText}>{this.state.expires}</Text>
</View>
</View>
{/* Token Issuer */}
<View style={styles.row}>
<View style={styles.label}>
<Text style={[styles.labelText]}>Issuer:</Text>
</View>
<View style={styles.value}>
<Text selectable style={styles.valueText}>
{this.state.iss}
</Text>
</View>
</View>
{/* Token Subject */}
<View style={styles.row}>
<View style={styles.label}>
<Text style={[styles.labelText]}>Subject:</Text>
</View>
<View style={styles.value}>
<Text selectable style={styles.valueText}>
{this.state.sub}
</Text>
</View>
</View>
{/* User Name */}
<View style={styles.row}>
<View style={styles.label}>
<Text style={[styles.labelText]}>Name:</Text>
</View>
<View style={styles.value}>
<Text selectable style={styles.valueText}>
{this.state.name}
</Text>
</View>
</View>
{/* E-mails */}
<View style={styles.row}>
<View style={styles.label}>
<Text style={[styles.labelText]}>E-mails:</Text>
</View>
<View style={styles.value}>
<Text selectable style={styles.valueText}>
{this.state.emails}
</Text>
</View>
</View>
</View>
<View style={styles.frame}>
<TouchableOpacity style={styles.button} onPress={this.onLogout}>
<Text style={styles.bodyText}>Logout</Text>
</TouchableOpacity>
</View>
</View>
);
}
}
// Define styles
const styles = StyleSheet.create({
body: {
flex: 1,
width: '100%',
backgroundColor: 'black',
alignItems: 'center',
justifyContent: 'center',
opacity: 0.92,
},
row: {
flexDirection: 'row',
borderTopWidth: 1,
borderColor: '#222',
},
label: {flex: 1, borderLeftWidth: 1, borderColor: '#222'},
value: {
flex: 2,
alignContent: 'flex-start',
borderLeftWidth: 1,
borderColor: '#222',
},
frame: {
backgroundColor: 'black',
alignItems: 'center',
justifyContent: 'center',
},
button: {
margin: 10,
padding: 10,
borderRadius: 5,
backgroundColor: '#222',
},
bodyText: {
color: 'white',
alignSelf: 'center',
},
valueText: {
color: 'white',
marginLeft: 2,
fontSize: 12,
},
labelText: {
color: 'white',
alignSelf: 'flex-start',
},
headerText: {
color: 'white',
paddingTop: 20,
fontSize: 18,
fontWeight: 'bold',
},
});