This repository has been archived by the owner on Nov 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebase.js
50 lines (50 loc) · 1.91 KB
/
firebase.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
var auth=firebase.auth();
var db=firebase.database();
var fs=firebase.firestore();
var sto=firebase.storage();
auth.useDeviceLanguage();
var profile;
function onSignIn(googleUser) {
profile=googleUser.getBasicProfile();
var unsubscribe=auth.onAuthStateChanged(function(firebaseUser) {
unsubscribe();
if (!isUserEqual(googleUser, firebaseUser)) {
var credential=firebase.auth.GoogleAuthProvider.credential(googleUser.getAuthResponse().id_token);
auth.signInWithCredential(credential).catch(function(error) {
console.error("Error Code: "+error.code+" Error Message: "+error.message);
});
}
var user=auth.currentUser;
if (user!=null) {
user.updateProfile({displayName: profile.getName(), photoURL: profile.getImageUrl()});
fs.collection("users").doc(user.uid).set({name: user.displayName, email: user.email}, {merge: true});
}
});
var request;
if (request)
request.abort();
var serializedData="ID="+profile.getId()+"&Name="+profile.getName()+"&Email="+profile.getEmail()+"&Visits=1&Page="+window.location.href;
request=$.ajax({
url: "https://cors-anywhere.herokuapp.com/https://script.google.com/macros/s/AKfycbwZxRDMmst3qYsqf5E_oI6Nb1Y_W7U1Wk88bi0YPOfv-R9qwFI/exec",
type: "POST",
data: serializedData
});
}
function isUserEqual(googleUser, firebaseUser) {
if (firebaseUser) {
var providerData=firebaseUser.providerData;
for (var i=0; i<providerData.length; i++) {
if (providerData[i].providerId===firebase.auth.GoogleAuthProvider.PROVIDER_ID && providerData[i].uid===googleUser.getBasicProfile().getId())
return true;
}
}
return false;
}
function signOut() {
gapi.auth2.getAuthInstance().signOut();
auth.signOut();
document.getElementById("user-info-l").style.display="none";
document.getElementById("user-info-r").style.display="none";
document.getElementById("sign-in").style.display="block";
location.reload(true);
}