-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathApp.js
152 lines (146 loc) · 4.93 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
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
TouchableOpacity,
ActivityIndicator
} from 'react-native';
import { LoginButton, AccessToken, LoginManager } from 'react-native-fbsdk';
import firebase from 'firebase';
const config = {
apiKey: "AIzaSyAfpxJrikhNNKy0p2s5a2XdGHh1PrNaKJ0",
authDomain: "fir-login-6af59.firebaseapp.com",
databaseURL: "https://fir-login-6af59.firebaseio.com",
storageBucket: "fir-login-6af59.appspot.com",
messagingSenderId: "86655694512"
};
firebase.initializeApp(config);
export default class facebook_login extends Component {
state = {
logged: false,
animating: false
}
handleLogin = () => {
if (!this.state.logged) {
LoginManager.logInWithPublishPermissions(['publish_actions'])
.then((result) => {
if (result.isCancelled) {
alert('Cancel login');
}
// } else if (result.declinedPermissions) {
// alert('declinedPermissions');
// } else {
this.setState({ logged: true });
AccessToken.getCurrentAccessToken().then(
(data) => {
alert(data.accessToken.toString())
}
).catch(error => alert(error));
})
.catch(error => console.log(error));
} else {
this.setState({ logged: false });
LoginManager.logOut();
}
}
onLogin = async () => {
try {
this.setState({
animating: true
});
const result = await LoginManager.logInWithReadPermissions(['public_profile', 'email']);
const tokenData = await AccessToken.getCurrentAccessToken();
const token = tokenData.accessToken.toString();
const credential = firebase.auth.FacebookAuthProvider.credential(token);
const user = await firebase.auth().signInWithCredential(credential);
firebase.database().ref(`/users/${user.uid}/profile`).set({
name: user.displayName,
email: user.email,
avatar: user.photoURL
});
this.setState({
animating: false
});
} catch (error) {
this.setState({
animating: false
});
console.log(error.message);
// do something here
}
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to React Native!
</Text>
<Text style={styles.instructions}>
To get started, edit index.android.js
</Text>
<ActivityIndicator
animating={this.state.animating}
color="#ddd"
size="large"
/>
<LoginButton
publishPermissions={["publish_actions"]}
onLoginFinished={
(error, result) => {
if (error) {
alert("login has error: " + result.error);
} else if (result.isCancelled) {
alert("login is cancelled.");
} else {
AccessToken.getCurrentAccessToken().then(
(data) => {
alert(data.accessToken.toString())
}
)
}
}
}
onLogoutFinished={() => alert("logout.")} />
<TouchableOpacity
onPress={this.onLogin}
style={{
marginTop: 10,
padding: 10,
backgroundColor: 'green',
borderRadius: 5,
}}
>
<Text style={{ color: '#fff' }}>
{this.state.logged ? 'Đăng xuất' : 'Đăng nhập'}
</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
AppRegistry.registerComponent('facebook_login', () => facebook_login);