forked from magus/react-native-facebook-login
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.android.js
168 lines (145 loc) · 4.19 KB
/
index.android.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import {
View,
Text,
StyleSheet,
NativeModules,
TouchableHighlight,
} from 'react-native';
import itypeof from 'itypeof';
const FBLoginManager = NativeModules.MFBLoginManager;
const styles = StyleSheet.create({
login: {
flex: 1,
backgroundColor: '#3B5998',
padding: 10,
alignItems: 'center'
},
whiteFont: {
color: 'white'
}
});
const statics = {
loginText: 'Login with Facebook',
logoutText: 'Logout from Facebook'
};
class FBLogin extends Component {
constructor (props) {
super(props);
this.login = this.login.bind(this);
this.logout = this.logout.bind(this);
this._handleEvent = this._handleEvent.bind(this);
this._getButtonView = this._getButtonView.bind(this);
this.getChildContext = this.getChildContext.bind(this);
this._onFacebookPress = this._onFacebookPress.bind(this);
this.state = {
statics:statics,
isLoggedIn: false,
buttonText: statics.loginText
};
}
componentDidMount(){
FBLoginManager.setLoginBehavior(this.props.loginBehavior)
.then((behaviour)=>{
console.log(`FbLogin: using ${behaviour.name} behaviour`, behaviour)
});
FBLoginManager.getCredentials((err, data) => {
if (!this.mounted) return;
if(data &&
itypeof(data.credentials) === 'object' &&
itypeof(data.credentials.token) === 'string' &&
data.credentials.token.length > 0) {
this.setState({isLoggedIn:true, buttonText: this.state.statics.logoutText});
} else {
this.setState({isLoggedIn:false, buttonText: this.state.statics.loginText});
}
this._handleEvent(null,data);
});
this.mounted = true;
}
componentWillUnmount(){
this.mounted = false;
}
static childContextTypes = {
isLoggedIn: PropTypes.bool,
login: PropTypes.func,
logout: PropTypes.func,
props: PropTypes.shape({})
}
getChildContext () {
return {
isLoggedIn: this.state.isLoggedIn,
login: this.login,
logout: this.logout,
props: this.props
};
}
login(permissions) {
FBLoginManager.loginWithPermissions(
permissions || this.props.permissions,
(err,data) => this._handleEvent(err,data)
);
}
logout() {
FBLoginManager.logout((err, data) => this._handleEvent(err, data));
}
_handleEvent(e, data) {
const result = e || data;
if(result.type === 'success' && result.profile){
try{
result.profile = JSON.parse(result.profile)
} catch (err) {
console.warn('Could not parse facebook profile: ', result.profile);
console.error(err);
}
}
if(result.eventName === 'onLogin' || result.eventName === 'onLoginFound'){
this.setState({isLoggedIn:true, buttonText: this.state.statics.logoutText});
} else if (result.eventName === 'onLogout'){
this.setState({isLoggedIn:false, buttonText: this.state.statics.loginText});
}
if(result.eventName && this.props.hasOwnProperty(result.eventName)){
const event = result.eventName;
delete result.eventName;
console.log('Triggering \'%s\' event', event)
this.props[event](result);
} else {
console.log('\'%s\' Event is not defined or recognized', result.eventName)
}
}
_onFacebookPress() {
let permissions = [];
if( itypeof(this.props.permissions) === 'array'){
permissions = this.props.permissions;
}
if(this.state.isLoggedIn){
this.logout()
}else{
this.login(permissions)
}
}
_getButtonView () {
const buttonText = this.props.facebookText ? this.props.facebookText:this.state.buttonText;
return (this.props.buttonView)
? this.props.buttonView
: (
<View style={[styles.login, this.props.style]}>
<Text style={[styles.whiteFont, this.fontStyle]}> {buttonText} </Text>
</View>
);
}
render(){
return (
<TouchableHighlight onPress={this._onFacebookPress} underlayColor={this.props.onClickColor} >
<View style={[this.props.containerStyle]}>
{this._getButtonView()}
</View>
</TouchableHighlight>
)
}
}
module.exports = {
FBLogin,
FBLoginManager
};