forked from GoogleWebComponents/firebase-element
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebase-auth.html
388 lines (356 loc) · 11.3 KB
/
firebase-auth.html
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<!--
@license
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<link rel="import" href="firebase.html">
<link rel="import" href="../polymer/polymer.html">
<!--
Element wrapper for the Firebase authentication API (https://www.firebase.com/docs/web/guide/user-auth.html).
-->
<script>
Polymer({
is: 'firebase-auth',
properties: {
/**
* Firebase location URL (must have simple login enabled via Forge interface).
*/
location: {
type: String,
reflectToAttribute: true,
observer: '_locationChanged'
},
/**
* Default login provider type. May be one of: `anonymous`, `custom`, `password`
* `facebook`, `github`, `twitter`, `google`.
*/
provider: {
type: String,
reflectToAttribute: true,
value: 'anonymous'
},
/**
* When logged in, this property reflects the firebase user auth object.
*/
user: {
type: Object,
readOnly: true,
notify: true
},
/**
* When true, login will be attempted if login status check determines no user is
* logged in. Should generally only be used with provider types that do not present
* a login UI, such as 'anonymous'.
*/
autoLogin: {
type: Boolean,
value: false,
reflectToAttribute: true
},
/**
* When true, login status can be determined by checking `user` property.
*/
statusKnown: {
type: Boolean,
value: false,
notify: true,
readOnly: true,
reflectToAttribute: true
},
/**
* When true, authentication will try to redirect instead of using a
* popup if possible.
*/
redirect: {
type: Boolean,
value: false,
reflectToAttribute: true
},
/**
* Provider-specific parameters to pass to login. May be overridden at `login()`-time.
*/
params: {
type: Object
},
/**
* Provider-specific options to pass to login, for provider types that take a second
* object to pass firebase-specific options. May be overridden at `login()`-time.
*/
options: {
type: Object
},
/**
* A pointer to the Firebase instance being used by the firebase-auth element.
*/
ref: {
type: Object,
readOnly: true,
notify: true
},
_boundAuthHandler: {
value: function() {
return this._authHandler.bind(this);
}
},
_boundOnlineHandler: {
value: function() {
return this._onlineHandler.bind(this);
}
},
_queuedLogin: {
type: Object
}
},
attached: function() {
window.addEventListener('online', this._boundOnlineHandler);
},
detached: function() {
window.removeEventListener('online', this._boundOnlineHandler);
this.ref.offAuth(this._boundAuthHandler);
},
_locationChanged: function(location) {
// Note: we debounce here in case location changed as part of property
// initialization, so that we don't try to login before other properties
// are initialized.
this.debounce('locationChanged', function() {
if (this.ref) {
this.ref.offAuth(this._boundAuthHandler);
}
if (location) {
this._setRef(new Firebase(location));
this.ref.onAuth(this._boundAuthHandler);
} else {
this._setRef(null);
}
}, 1);
},
_loginHandler: function(error, user) {
if (error) {
// an error occurred while attempting login
this.fire('error', error);
} else {
this._authHandler(user);
}
},
_authHandler: function(user) {
if (user) {
// user authenticated with Firebase
this._setUser(user);
this._setStatusKnown(true);
this.fire('login', {user: user});
} else {
this._setUser(null);
if (this.statusKnown) {
this._setStatusKnown(false);
this.fire('logout');
}
if (this._queuedLogin) {
this.login(this._queuedLogin.params, this._queuedLogin.options);
this._queuedLogin = null;
} else if (!this.statusKnown && this.autoLogin) {
this.login();
}
this._setStatusKnown(true);
}
},
/**
* Performs a login attempt, using the `provider` specified via attribute/property,
* or optionally via `provider` argument to the `login` function. Optionally,
* provider-specific login parameters can be specified via attribute (JSON)/property,
* or via the `params` argument to the `login` function.
*
* If the login is successful, the `login` event is fired, with `e.detail.user`
* containing the authenticated user object from Firebase.
*
* If login fails, the `error` event is fired, with `e.detail` containing error
* information supplied from Firebase.
*
* If the browswer supports `navigator.onLine` network status reporting and the
* network is currently offline, the login attempt will be queued until the network
* is restored.
*
* @method login
* @param {string} params (optional)
* @param {string} options (optional)
*/
login: function(params, options) {
if (!this.ref || navigator.onLine === false) {
this._queuedLogin = {params: params, options: options};
} else {
params = params || this.params || undefined;
options = options || this.options || undefined;
switch(this.provider) {
case 'password':
this.ref.authWithPassword(params, this._loginHandler.bind(this), options);
break;
case 'anonymous':
this.ref.authAnonymously(this._loginHandler.bind(this), params);
break;
case 'custom':
this.ref.authWithCustomToken(params.token, this._loginHandler.bind(this));
break;
case 'facebook':
case 'google':
case 'github':
case 'twitter':
if (this.redirect) {
this.ref.authWithOAuthRedirect(this.provider, this._loginHandler.bind(this), params);
} else {
this.ref.authWithOAuthPopup(this.provider, this._loginHandler.bind(this), params);
}
break;
default:
throw 'Unknown provider: ' + this.provider;
}
}
},
/**
* Performs a logout attempt.
*
* If the login is successful, the `logout` event is fired.
*
* If login fails, the `error` event is fired, with `e.detail` containing error
* information supplied from Firebase.
*
* If the browswer supports `navigator.onLine` network status reporting and the
* network is currently offline, the logout attempt will be queued until the network
* is restored.
*
* @method logout
*/
logout: function() {
if (navigator.onLine === false) {
this.queuedLogout = true;
} else {
this.ref.unauth();
}
},
_onlineHandler: function() {
if (this.queuedLogout) {
this.queuedLogout = false;
this.logout();
} else if (this.queuedLogin) {
this.login(this.queuedLogin.params, this.queuedLogin.options);
this.queuedLogin = null;
}
},
/**
* Creates a "password provider"-based user account.
*
* If the operation is successful, the `user-created` event is fired.
*
* If the operation fails, the `error` event is fired, with `e.detail`
* containing error information supplied from Firebase.
*
* @method createUser
* @param {string} email
* @param {string} password
*/
createUser: function(email, password) {
this.ref.createUser({email: email, password: password}, function(error) {
if (!error) {
this.fire('user-created');
} else {
this.fire('error', error);
}
}.bind(this));
},
/**
* Changes the password of a "password provider"-based user account.
*
* If the operation is successful, the `password-changed` event is fired.
*
* If the operation fails, the `error` event is fired, with `e.detail`
* containing error information supplied from Firebase.
*
* @method changePassword
* @param {string} email
* @param {string} oldPassword
* @param {string} newPassword
*/
changePassword: function(email, oldPassword, newPassword) {
this.ref.changePassword({
email: email,
oldPassword: oldPassword,
newPassword: newPassword
}, function(error) {
if (!error) {
this.fire('password-changed');
} else {
this.fire('error', error);
}
}.bind(this));
},
/**
* Sends a password reset email for a "password provider"-based user account.
*
* If the operation is successful, the `password-reset` event is fired.
*
* If the operation fails, the `error` event is fired, with `e.detail`
* containing error information supplied from Firebase.
*
* @method sendPasswordResetEmail
* @param {string} email
*/
sendPasswordResetEmail: function(email) {
this.ref.resetPassword({email: email}, function(error) {
if (!error) {
this.fire('password-reset');
} else {
this.fire('error', error);
}
}.bind(this));
},
/**
* Changes the email of a "password provider"-based user account.
*
* If the operation is successful, the `email-changed` event is fired.
*
* If the operation fails, the `error` event is fired, with `e.detail`
* containing error information supplied from Firebase.
*
* @method changeEmail
* @param {string} oldEmail
* @param {string} newEmail
* @param {string} Password
*/
changeEmail: function(oldEmail, newEmail, password) {
this.ref.changeEmail({
oldEmail: oldEmail,
newEmail: newEmail,
password: password
}, function(error) {
if (!error) {
this.fire('email-changed');
} else {
this.fire('error', error);
}
}.bind(this));
},
/**
* Removes a "password provider"-based user account.
*
* If the operation is successful, the `user-removed` event is fired.
*
* If the operation fails, the `error` event is fired, with `e.detail`
* containing error information supplied from Firebase.
*
* @method removeUser
* @param {string} email
* @param {string} password
*/
removeUser: function(email, password) {
this.ref.removeUser({email: email, password: password}, function(error, success) {
if (!error) {
this.fire('user-removed');
} else {
this.fire('error', error);
}
}.bind(this));
}
});
</script>