-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebase.js
215 lines (200 loc) · 9.19 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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* Firebase authentication, database and storage functions
*/
HOVERBOARD.Firebase = HOVERBOARD.Firebase || (function () {
'use strict';
// Sets up shortcuts to Firebase features and initiate firebase auth.
var initFirebase = function() {
// Shortcuts to Firebase SDK features.
this.auth = firebase.auth();
this.database = firebase.database();
this.storage = firebase.storage();
//this.messaging = firebase.messaging();
// Initiates Firebase auth and listen to auth state changes.
this.ratingsRef = null;
//this.messagesRef = null;
this.auth.onAuthStateChanged(this.onAuthStateChanged.bind(this));
this.provider = null;
this.pendingCred = null;
this.isAnonymous = false;
this.anonymousUser = null;
}
var getProviderForProviderStr = function (providerStr) {
switch (providerStr) {
case "google": return firebase.auth.GoogleAuthProvider.PROVIDER_ID;
case "facebook": return firebase.auth.FacebookAuthProvider.PROVIDER_ID;
case "twitter": return firebase.auth.TwitterAuthProvider.PROVIDER_ID;
case "github": return firebase.auth.GithubAuthProvider.PROVIDER_ID;
}
return null;
}
var getProviderForProviderId = function(providerId) {
switch(providerId) {
case firebase.auth.GoogleAuthProvider.PROVIDER_ID: return new firebase.auth.GoogleAuthProvider();
case firebase.auth.FacebookAuthProvider.PROVIDER_ID: return new firebase.auth.FacebookAuthProvider();
case firebase.auth.TwitterAuthProvider.PROVIDER_ID: return new firebase.auth.TwitterAuthProvider();
case firebase.auth.GithubAuthProvider.PROVIDER_ID: return new firebase.auth.GithubAuthProvider();
}
return null;
}
var signInAnonymously = function() {
firebase.auth().signInAnonymously().catch(function(error) {
HOVERBOARD.Analytics.trackEvent('firebase', 'error_sign_in_anonymously', error.code + ': ' + error.message);
// Handle Errors here.
console.log(error.code + ': ' + error.message);
setTimeout(function() {
this.signInAnonymously();
}.bind(this), 5000);
}.bind(this));
}
// Signs-in
var signIn = function(providerId) {
var provider = getProviderForProviderId(providerId);
return this.auth.signInWithPopup(provider).catch(function(error) {
// An error happened.
if (error.code === 'auth/account-exists-with-different-credential') {
HOVERBOARD.Analytics.trackEvent('firebase', 'sign_in_attempt_with_other_credential', providerId);
// Step 2.
// User's email already exists.
// The pending GitHub credential.
this.pendingCred = error.credential;
// The provider account's email address.
var email = error.email;
// Get registered providers for this email.
this.auth.fetchProvidersForEmail(email).then(function(providers) {
// Step 3.
if (providers[0] === 'password') {
HOVERBOARD.Analytics.trackEvent('firebase', 'sign_in_attempt_with_email_password', 'notimplemented');
HOVERBOARD.Elements.Template.$.toast.showMessage("Not yet implemented");
// Asks the user his password.
/// TODO: not implemented
return;
}
// All the other cases are external providers.
this.provider = providers[0];
HOVERBOARD.Analytics.trackEvent('firebase', 'sign_in_prompting_for_redirect', providerId);
HOVERBOARD.Elements.Template.openAuthRedirect();
// ... redirectedSignIn
}.bind(this));
}
}.bind(this)).then(function(userCred) {
// TODO: linking this way doesn't work. We'd need to link before signInWithPopup flow (https://firebase.google.com/docs/auth/web/anonymous-auth)
// But that works only for Google and Facebook seemengly.
// if (this.anonymousUser) {
// this.anonymousUser.link(userCred).then(function(user) {
// HOVERBOARD.Analytics.trackEvent('firebase', 'sign_in_anonymous_account_linked', providerId);
// }, function(error) {
// HOVERBOARD.Analytics.trackEvent('firebase', 'error_sign_in_anonymous_account_linking', providerId);
// });
// }
HOVERBOARD.Analytics.trackEvent('firebase', 'sign_in_worked_first_shot', userCred.user.uid, providerId);
}.bind(this));
}
var redirectedSignIn = function() {
var provider = getProviderForProviderId(this.provider);
// Sign in to provider. Note: browsers usually block popup triggered asynchronously,
// so in real scenario you should ask the user to click on a "continue" button
// that will trigger the signInWithPopup.
HOVERBOARD.Analytics.trackEvent('firebase', 'sign_in_redirecting', this.provider);
this.auth.signInWithPopup(provider).then(function(result) {
// Remember that the user may have signed in with an account that has a different email
// address than the first one. This can happen as Firebase doesn't control the provider's
// sign in flow and the user is free to login using whichever account he owns.
// Step 4b.
// Link to credential.
// As we have access to the pending credential, we can directly call the link method.
result.user.link(this.pendingCred).then(function() {
// Account successfully linked to the existing Firebase user.
HOVERBOARD.Analytics.trackEvent('firebase', 'sign_in_account_linked', this.provider);
}.bind(this));
}.bind(this));
}
// Signs-out
var signOut = function(sourceComponentName) {
HOVERBOARD.Analytics.trackEvent('firebase', 'sign_out_attempt', sourceComponentName);
// Sign out of Firebase.
return this.auth.signOut().then(function() {
// Sign-out successful.
}, function(error) {
HOVERBOARD.Analytics.trackEvent('firebase', 'signout_error', 'firebase');
HOVERBOARD.Elements.Template.$.toast.showMessage("Couldn't sign out");
});
}
var onAuthStateChanged = function(user) {
if (user) { // User is signed in!
if (user.isAnonymous)
this.anonymousUser = user;
this.isAnonymous = user.isAnonymous;
HOVERBOARD.Analytics.trackEvent('firebase', user.isAnonymous ? 'signed_in_anonymously' : 'signed_in', 'handler');
this.ratingsRef = this.database.ref('ratings');
HOVERBOARD.Elements.Template.adjustSignedIn(user.isAnonymous ? null : user);
/*
// Reference to the /messages/ database path.
this.messagesRef = this.database.ref('messages');
// Make sure we remove all previous listeners.
this.messagesRef.off();
// Loads the last 12 messages and listen for new ones.
var showMessage = function(data) {
var val = data.val();
HOVERBOARD.Elements.Template.$.toast.showMessage(val.text);
//this.displayMessage(val.text);
}.bind(this);
this.messagesRef.limitToLast(2).on('child_added', showMessage);
*/
} else { // User is signed out!
this.isAnonymous = false;
this.anonymousUser = null;
HOVERBOARD.Analytics.trackEvent('firebase', 'signed_out', 'handler');
this.ratingsRef = null;
HOVERBOARD.Elements.Template.adjustSignedIn(null);
}
}
/*
var broadcast = function(text) {
this.messagesRef.push({text: text});
}
*/
// Returns true if user is signed-in. Otherwise false.
var checkSignedIn = function() {
return this.auth ? this.auth.currentUser : null;
}
// Returns true if user is signed-in. Otherwise false and displays a message.
var checkSignedInWithMessage = function() {
if (this.checkSignedIn()) {
return true;
}
HOVERBOARD.Elements.Template.$.toast.showMessage('Please sign-in first');
return false;
}
var submitRatingData = function(sessionId, ratingData) {
// Add a new rating entry to the Firebase Database.
var user = this.auth.currentUser;
var ratingPayload = ratingData;
ratingPayload.session = sessionId;
ratingPayload.name = user.displayName;
ratingPayload.session = sessionId;
ratingPayload.uid = user.uid;
ratingPayload.email = user.email;
ratingPayload.time = Date.now();
this.ratingsRef.push(ratingPayload).then(function() {
HOVERBOARD.Analytics.trackEvent('session', 'rating', sessionId);
HOVERBOARD.Elements.Template.$.toast.showMessage('Submission recorded');
}.bind(this)).catch(function(error) {
HOVERBOARD.Analytics.trackEvent('session', 'rating_failure', 'writing', sessionId);
console.error('Error writing rating to Firebase Database', error);
});
}
return {
initFirebase: initFirebase,
getProviderForProviderStr: getProviderForProviderStr,
signIn: signIn,
signInAnonymously: signInAnonymously,
redirectedSignIn: redirectedSignIn,
signOut: signOut,
onAuthStateChanged: onAuthStateChanged,
submitRatingData: submitRatingData,
checkSignedIn: checkSignedIn,
checkSignedInWithMessage: checkSignedInWithMessage//,
//broadcast: broadcast
};
}());