-
Notifications
You must be signed in to change notification settings - Fork 250
/
Copy pathfacebook.js
163 lines (148 loc) · 5.21 KB
/
facebook.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
// These samples are intended for Web so this import would normally be
// done in HTML however using modules here is more convenient for
// ensuring sample correctness offline.
import firebase from "firebase/app";
import "firebase/auth";
function facebookProvider() {
// [START auth_facebook_provider_create]
var provider = new firebase.auth.FacebookAuthProvider();
// [END auth_facebook_provider_create]
// / [START auth_facebook_provider_scopes]
provider.addScope('user_birthday');
// [END auth_facebook_provider_scopes]
// [START auth_facebook_provider_params]
provider.setCustomParameters({
'display': 'popup'
});
// [END auth_facebook_provider_params]
}
function facebookSignInPopup(provider) {
// [START auth_facebook_signin_popup]
firebase
.auth()
.signInWithPopup(provider)
.then((result) => {
/** @type {firebase.auth.OAuthCredential} */
var credential = result.credential;
// The signed-in user info.
var user = result.user;
// IdP data available in result.additionalUserInfo.profile.
// ...
// This gives you a Facebook Access Token. You can use it to access the Facebook API.
var accessToken = credential.accessToken;
// ...
})
.catch((error) => {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
// ...
});
// [END auth_facebook_signin_popup]
}
function facebookSignInRedirectResult() {
// [START auth_facebook_signin_redirect_result]
firebase.auth()
.getRedirectResult()
.then((result) => {
if (result.credential) {
/** @type {firebase.auth.OAuthCredential} */
var credential = result.credential;
// This gives you a Facebook Access Token. You can use it to access the Facebook API.
var token = credential.accessToken;
// ...
}
// The signed-in user info.
var user = result.user;
// IdP data available in result.additionalUserInfo.profile.
// ...
}).catch((error) => {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
// ...
});
// [END auth_facebook_signin_redirect_result]
}
// [START auth_facebook_callback]
function checkLoginState(response) {
if (response.authResponse) {
// User is signed-in Facebook.
var unsubscribe = firebase.auth().onAuthStateChanged((firebaseUser) => {
unsubscribe();
// Check if we are already signed-in Firebase with the correct user.
if (!isUserEqual(response.authResponse, firebaseUser)) {
// Build Firebase credential with the Facebook auth token.
var credential = firebase.auth.FacebookAuthProvider.credential(
response.authResponse.accessToken);
// Sign in with the credential from the Facebook user.
firebase.auth().signInWithCredential(credential)
.catch((error) => {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
// ...
});
} else {
// User is already signed-in Firebase with the correct user.
}
});
} else {
// User is signed-out of Facebook.
firebase.auth().signOut();
}
}
// [END auth_facebook_callback]
// [START auth_facebook_checksameuser]
function isUserEqual(facebookAuthResponse, firebaseUser) {
if (firebaseUser) {
var providerData = firebaseUser.providerData;
for (var i = 0; i < providerData.length; i++) {
if (providerData[i].providerId === firebase.auth.FacebookAuthProvider.PROVIDER_ID &&
providerData[i].uid === facebookAuthResponse.userID) {
// We don't need to re-auth the Firebase connection.
return true;
}
}
}
return false;
}
// [END auth_facebook_checksameuser]
function authWithCredential(credential) {
// [START auth_facebook_signin_credential]
// Sign in with the credential from the Facebook user.
firebase.auth().signInWithCredential(credential)
.then((result) => {
// Signed in
var credential = result.credential;
// ...
})
.catch((error) => {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// The email of the user's account used.
var email = error.email;
// The firebase.auth.AuthCredential type that was used.
var credential = error.credential;
// ...
});
// [END auth_facebook_signin_credential]
}
function facebookProviderCredential(accessToken) {
// [START auth_facebook_provider_credential]
var credential = firebase.auth.FacebookAuthProvider.credential(accessToken);
// [END auth_facebook_provider_credential]
}