-
Notifications
You must be signed in to change notification settings - Fork 5
/
App.js
139 lines (130 loc) · 3.71 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
139
import React, { Component } from 'react';
import {
Alert,
AppRegistry,
Button,
Platform,
StyleSheet,
Text,
View,
FlatList
} from 'react-native';
import AzureAuth from 'react-native-azure-auth';
import Client from 'react-native-azure-auth/src/networking';
const CLIENT_ID = 'b5d120f6-04fb-481a-88f6-XXXXXXXX' // replace the string with YOUR client ID
const azureAuth = new AzureAuth({
clientId: CLIENT_ID
});
export default class Auth0Sample extends Component {
constructor(props) {
super(props);
this.state = { accessToken: null, user: '' , mails: [], userId: ''};
}
_onLogin = async () => {
try {
let tokens = await azureAuth.webAuth.authorize({scope: 'openid profile User.Read' })
console.log('CRED>>>', tokens)
this.setState({ accessToken: tokens.accessToken });
let info = await azureAuth.auth.msGraphRequest({token: tokens.accessToken, path: 'me'})
console.log('info', info)
this.setState({ user: info.displayName, userId: tokens.userId })
} catch (error) {
console.log('Error during Azure operation', error)
}
};
_onGetMails = async () => {
try {
let tokens = await azureAuth.auth.acquireTokenSilent({scope: 'Mail.Read', userId: this.state.userId})
console.log('Silent:', tokens)
if (!tokens) {
tokens = await azureAuth.webAuth.authorize({scope: 'Mail.Read'})
console.log('NewWeb:', tokens)
}
console.log('TOK>>>', tokens.accessToken)
let mails = await azureAuth.auth.msGraphRequest({token: tokens.accessToken, path: '/me/mailFolders/Inbox/messages'})
let mailArr = []
mails.value.forEach(element => {
mailArr.push({subject: element.subject})
});
if (mailArr.length === 0) {
mailArr.push({subject: 'No mails found'})
}
console.log('Mails: ' + mailArr.length)
this.setState({mails: mailArr})
} catch (error) {
console.log(error)
}
}
_onLogout = () => {
azureAuth.webAuth
.clearSession()
.then(success => {
this.setState({ accessToken: null, user: null });
})
.catch(error => console.log(error));
};
render() {
let loggedIn = this.state.accessToken ? true : false;
return (
<View style={styles.container}>
<View>
<Text style={styles.header}>Azure Auth - Login</Text>
<Text style={styles.text}>Hello {this.state.user}!</Text>
<Text style={styles.text}>
You are {loggedIn ? '' : 'not '}logged in.
</Text>
</View>
<View style={styles.buttons}>
<Button
style={styles.button}
onPress={loggedIn ? this._onLogout : this._onLogin}
title={loggedIn ? 'Log Out' : 'Log In'}
/>
<Button
style={styles.button}
onPress={this._onGetMails}
disabled={!loggedIn}
title={'Get E-Mails'}
/>
</View>
<FlatList style={styles.list}
data={this.state.mails}
renderItem={({item}) => <Text style={{padding: 10}}>{item.subject}</Text>}
keyExtractor={(item, index) => `key-${index}`}
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'stretch',
backgroundColor: '#F5FCFF'
},
header: {
fontSize: 20,
textAlign: 'center',
margin: 10
},
text: {
textAlign: 'center'
},
buttons: {
flexDirection: 'row',
justifyContent: 'space-around',
alignItems: 'baseline',
padding: 20
},
button: {
flex: 1,
padding:20,
margin:20
},
list: {
flex: 5,
margin:20
}
});
AppRegistry.registerComponent('Auth0Sample', () => Auth0Sample);