forked from magus/react-native-facebook-login
/
index.ios.js
101 lines (86 loc) · 2.4 KB
/
index.ios.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
var React = require('react-native');
var {
View,
StyleSheet,
PropTypes,
NativeModules,
requireNativeComponent,
NativeMethodsMixin,
DeviceEventEmitter,
} = React;
var { FBLoginManager } = NativeModules;
var FBLogin = React.createClass({
statics: {
Events: FBLoginManager.Events,
},
propTypes: {
style: View.propTypes.style,
permissions: PropTypes.array, // default: ["public_profile", "email"]
loginBehavior: PropTypes.number, // default: Native
onLogin: PropTypes.func,
onLogout: PropTypes.func,
onLoginFound: PropTypes.func,
onLoginNotFound: PropTypes.func,
onError: PropTypes.func,
onCancel: PropTypes.func,
onPermissionsMissing: PropTypes.func,
},
getInitialState: function(){
return {
credentials: null,
subscriptions: [],
};
},
mixins: [NativeMethodsMixin],
componentWillMount: function(){
var _this = this;
var subscriptions = this.state.subscriptions;
// For each event key in FBLoginManager constantsToExport
// Create listener and call event handler from props
// e.g. this.props.onError, this.props.onLogin
Object.keys(FBLoginManager.Events).forEach(function(event){
subscriptions.push(DeviceEventEmitter.addListener(
FBLoginManager.Events[event],
(eventData) => {
// event handler defined? call it and pass along any event data
var eventHandler = _this.props["on"+event];
eventHandler && eventHandler(eventData);
}
));
})
// Add listeners to state
this.setState({ subscriptions : subscriptions });
},
componentWillUnmount: function(){
var subscriptions = this.state.subscriptions;
subscriptions.forEach(function(subscription){
subscription.remove();
});
},
componentDidMount: function(){
var _this = this;
FBLoginManager.getCredentials(function(error, data){
if( !_this.isMounted() ) return;
if (!error) {
_this.setState({ credentials : data.credentials });
} else {
_this.setState({ credentials : null });
}
});
},
render: function() {
var props = {
...this.props,
style: ([styles.base, this.props.style]),
};
return <RCTFBLogin {...props} />
},
});
var RCTFBLogin = requireNativeComponent('RCTFBLogin', FBLogin);
var styles = StyleSheet.create({
base: {
width: 175,
height: 30,
},
});
module.exports = FBLogin;