forked from wkh237/rn-azure-ad-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.common.js
147 lines (135 loc) · 3.83 KB
/
index.common.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
140
141
142
143
144
145
146
147
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
TouchableOpacity,
View
} from 'react-native';
import {ReactNativeAD, ADLoginView} from 'react-native-azure-ad'
const CLIENT_ID = 'xxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx'
const AUTH_URL = 'https://login.microsoftonline.com/common/oauth2/authorize'
const ADContext = new ReactNativeAD({
client_id : CLIENT_ID,
// redirectUrl : 'http://localhost:8080',
// Optional
authority_host : AUTH_URL,
// Optional
// tenant : 'common',
// This is required if client_id is a web application id
// but not recommended doing this way.
// client_secret : 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
resources : [
'https://graph.microsoft.com',
]
})
class rn_ad_sample extends Component {
constructor(props) {
super(props)
this.state = {
// this property will store user credential after logged in.
info : null,
// logout if this is true
shouldLogout : false,
// for display different views
displayType : 'before_login'
}
}
render() {
return (
<View style={styles.container}>
{this._renderContent.bind(this)()}
</View>
);
}
_renderContent() {
switch(this.state.displayType) {
case 'before_login' :
return <TouchableOpacity style={styles.button}
onPress={(this._showADLogin.bind(this))}>
<Text style={{color : 'white'}}>Login</Text>
</TouchableOpacity>
case 'login' :
// In fact we care if it successfully redirect to the URI, because
// we alread have the access_token after successfully logged in.
// set `hideAfterLogin` to `true` so that it won't display an error page.
return [
<ADLoginView
key="webview"
hideAfterLogin={true}
style={{flex :1}}
needLogout={this.state.shouldLogout}
context={ADContext}
onURLChange={this._onURLChange.bind(this)}
onSuccess={this._onLoginSuccess.bind(this)}/>]
case 'after_login' :
return [
<Text key="text">You're logged in as {this.state.info} </Text>,
<TouchableOpacity key="button" style={styles.button}
onPress={(this._logout.bind(this))}>
<Text style={{color : 'white'}}>Logout</Text>
</TouchableOpacity>]
break
}
}
_onURLChange(e) {
// listen to webview URL change, if the URL matches login URL redirect user
// to start page.
let isLoginPage = e.url === `${AUTH_URL}?response_type=code&client_id=${CLIENT_ID}`
if(isLoginPage && this.state.shouldLogout) {
console.log('logged out')
this.setState({
displayType : 'before_login',
shouldLogout : false
})
}
}
_showADLogin() {
this.setState({
displayType : 'login'
})
}
_logout() {
this.setState({
displayType : 'login',
shouldLogout : true
})
}
_onLoginSuccess(cred) {
console.log('user credential', cred)
let access_token = ADContext.getAccessToken('https://graph.microsoft.com')
fetch('https://graph.microsoft.com/beta/me', {
method : 'GET',
headers : {
Authorization : `bearer ${access_token}`
}
})
.then(res => res.json())
.then(user => {
console.log(user)
this.setState({
displayType : 'after_login',
info : user.displayName
})
})
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
button : {
margin : 24,
backgroundColor : '#1a6ed1',
padding : 12
},
});
AppRegistry.registerComponent('rn_ad_sample', () => rn_ad_sample);