| OLD | NEW |
| (Empty) | |
| 1 <!-- |
| 2 @license |
| 3 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. |
| 4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt |
| 5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
| 6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt |
| 7 Code distributed by Google as part of the polymer project is also |
| 8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt |
| 9 --> |
| 10 <link rel="import" href="firebase.html"> |
| 11 <link rel="import" href="../polymer/polymer.html"> |
| 12 |
| 13 <!-- |
| 14 Element wrapper for the Firebase authentication API (https://www.firebase.com/do
cs/web/guide/user-auth.html). |
| 15 --> |
| 16 |
| 17 <script> |
| 18 Polymer({ |
| 19 is: 'firebase-auth', |
| 20 |
| 21 properties: { |
| 22 /** |
| 23 * Firebase location URL (must have simple login enabled via Forge interfa
ce). |
| 24 */ |
| 25 location: { |
| 26 type: String, |
| 27 reflectToAttribute: true, |
| 28 observer: '_locationChanged' |
| 29 }, |
| 30 |
| 31 /** |
| 32 * Default login provider type. May be one of: `anonymous`, `custom`, `pa
ssword` |
| 33 * `facebook`, `github`, `twitter`, `google`. |
| 34 */ |
| 35 provider: { |
| 36 type: String, |
| 37 reflectToAttribute: true, |
| 38 value: 'anonymous' |
| 39 }, |
| 40 |
| 41 /** |
| 42 * When logged in, this property reflects the firebase user auth object. |
| 43 */ |
| 44 user: { |
| 45 type: Object, |
| 46 readOnly: true, |
| 47 notify: true |
| 48 }, |
| 49 |
| 50 /** |
| 51 * When true, login will be attempted if login status check determines no
user is |
| 52 * logged in. Should generally only be used with provider types that do n
ot present |
| 53 * a login UI, such as 'anonymous'. |
| 54 */ |
| 55 autoLogin: { |
| 56 type: Boolean, |
| 57 value: false, |
| 58 reflectToAttribute: true |
| 59 }, |
| 60 |
| 61 /** |
| 62 * When true, login status can be determined by checking `user` property. |
| 63 */ |
| 64 statusKnown: { |
| 65 type: Boolean, |
| 66 value: false, |
| 67 notify: true, |
| 68 readOnly: true, |
| 69 reflectToAttribute: true |
| 70 }, |
| 71 |
| 72 /** |
| 73 * When true, authentication will try to redirect instead of using a |
| 74 * popup if possible. |
| 75 */ |
| 76 redirect: { |
| 77 type: Boolean, |
| 78 value: false, |
| 79 reflectToAttribute: true |
| 80 }, |
| 81 |
| 82 /** |
| 83 * Provider-specific parameters to pass to login. May be overridden at `l
ogin()`-time. |
| 84 */ |
| 85 params: { |
| 86 type: Object |
| 87 }, |
| 88 |
| 89 /** |
| 90 * Provider-specific options to pass to login, for provider types that tak
e a second |
| 91 * object to pass firebase-specific options. May be overridden at `login(
)`-time. |
| 92 */ |
| 93 options: { |
| 94 type: Object |
| 95 }, |
| 96 |
| 97 /** |
| 98 * A pointer to the Firebase instance being used by the firebase-auth elem
ent. |
| 99 */ |
| 100 ref: { |
| 101 type: Object, |
| 102 readOnly: true, |
| 103 notify: true |
| 104 }, |
| 105 |
| 106 _boundAuthHandler: { |
| 107 value: function() { |
| 108 return this._authHandler.bind(this); |
| 109 } |
| 110 }, |
| 111 |
| 112 _boundOnlineHandler: { |
| 113 value: function() { |
| 114 return this._onlineHandler.bind(this); |
| 115 } |
| 116 }, |
| 117 |
| 118 _queuedLogin: { |
| 119 type: Object |
| 120 } |
| 121 }, |
| 122 |
| 123 attached: function() { |
| 124 window.addEventListener('online', this._boundOnlineHandler); |
| 125 }, |
| 126 |
| 127 detached: function() { |
| 128 window.removeEventListener('online', this._boundOnlineHandler); |
| 129 this.ref.offAuth(this._boundAuthHandler); |
| 130 }, |
| 131 |
| 132 _locationChanged: function(location) { |
| 133 if (this.ref) { |
| 134 this.ref.offAuth(this._boundAuthHandler); |
| 135 } |
| 136 |
| 137 if (location) { |
| 138 this._setRef(new Firebase(location)); |
| 139 this.ref.onAuth(this._boundAuthHandler); |
| 140 } else { |
| 141 this._setRef(null) |
| 142 } |
| 143 }, |
| 144 |
| 145 _loginHandler: function(error, user) { |
| 146 if (error) { |
| 147 // an error occurred while attempting login |
| 148 this.fire('error', error); |
| 149 } else { |
| 150 this._authHandler(user); |
| 151 } |
| 152 }, |
| 153 |
| 154 _authHandler: function(user) { |
| 155 if (user) { |
| 156 // user authenticated with Firebase |
| 157 this._setUser(user); |
| 158 this._setStatusKnown(true); |
| 159 this.fire('login', {user: user}); |
| 160 } else { |
| 161 this._setUser(null); |
| 162 |
| 163 if (this.statusKnown) { |
| 164 this._setStatusKnown(false); |
| 165 this.fire('logout'); |
| 166 } |
| 167 |
| 168 if (this._queuedLogin) { |
| 169 this.login(this._queuedLogin.params, this._queuedLogin.options); |
| 170 this._queuedLogin = null; |
| 171 } else if (!this.statusKnown && this.autoLogin) { |
| 172 this.login(); |
| 173 } |
| 174 |
| 175 this._setStatusKnown(true); |
| 176 } |
| 177 }, |
| 178 |
| 179 /** |
| 180 * Performs a login attempt, using the `provider` specified via attribute/pr
operty, |
| 181 * or optionally via `provider` argument to the `login` function. Optionall
y, |
| 182 * provider-specific login parameters can be specified via attribute (JSON)/
property, |
| 183 * or via the `params` argument to the `login` function. |
| 184 * |
| 185 * If the login is successful, the `login` event is fired, with `e.detail.us
er` |
| 186 * containing the authenticated user object from Firebase. |
| 187 * |
| 188 * If login fails, the `error` event is fired, with `e.detail` containing er
ror |
| 189 * information supplied from Firebase. |
| 190 * |
| 191 * If the browswer supports `navigator.onLine` network status reporting and
the |
| 192 * network is currently offline, the login attempt will be queued until the
network |
| 193 * is restored. |
| 194 * |
| 195 * @method login |
| 196 * @param {string} params (optional) |
| 197 * @param {string} options (optional) |
| 198 */ |
| 199 login: function(params, options) { |
| 200 if (navigator.onLine === false) { |
| 201 this._queuedLogin = {params: params, options: options}; |
| 202 } else { |
| 203 params = params || (this.params && JSON.parse(this.params)) || undefined
; |
| 204 options = options || (this.options && JSON.parse(this.options)) || undef
ined; |
| 205 switch(this.provider) { |
| 206 case 'password': |
| 207 this.ref.authWithPassword(params, this._loginHandler.bind(this), opt
ions); |
| 208 break; |
| 209 case 'anonymous': |
| 210 this.ref.authAnonymously(this._loginHandler.bind(this), params); |
| 211 break; |
| 212 case 'custom': |
| 213 this.ref.authWithCustomToken(params.token, this._loginHandler.bind(t
his)); |
| 214 break; |
| 215 case 'facebook': |
| 216 case 'google': |
| 217 case 'github': |
| 218 case 'twitter': |
| 219 if (this.redirect) { |
| 220 this.ref.authWithOAuthRedirect(this.provider, this._loginHandler.b
ind(this), params); |
| 221 } else { |
| 222 this.ref.authWithOAuthPopup(this.provider, this._loginHandler.bind
(this), params); |
| 223 } |
| 224 break; |
| 225 default: |
| 226 throw 'Unknown provider: ' + this.provider; |
| 227 } |
| 228 } |
| 229 }, |
| 230 |
| 231 /** |
| 232 * Performs a logout attempt. |
| 233 * |
| 234 * If the login is successful, the `logout` event is fired. |
| 235 * |
| 236 * If login fails, the `error` event is fired, with `e.detail` containing er
ror |
| 237 * information supplied from Firebase. |
| 238 * |
| 239 * If the browswer supports `navigator.onLine` network status reporting and
the |
| 240 * network is currently offline, the logout attempt will be queued until the
network |
| 241 * is restored. |
| 242 * |
| 243 * @method logout |
| 244 */ |
| 245 logout: function() { |
| 246 if (navigator.onLine === false) { |
| 247 this.queuedLogout = true; |
| 248 } else { |
| 249 this.ref.unauth(); |
| 250 } |
| 251 }, |
| 252 |
| 253 _onlineHandler: function() { |
| 254 if (this.queuedLogout) { |
| 255 this.queuedLogout = false; |
| 256 this.logout(); |
| 257 } else if (this.queuedLogin) { |
| 258 this.login(this.queuedLogin.params, this.queuedLogin.options); |
| 259 this.queuedLogin = null; |
| 260 } |
| 261 }, |
| 262 |
| 263 /** |
| 264 * Creates a "password provider"-based user account. |
| 265 * |
| 266 * If the operation is successful, the `user-created` event is fired. |
| 267 * |
| 268 * If the operation fails, the `error` event is fired, with `e.detail` |
| 269 * containing error information supplied from Firebase. |
| 270 * |
| 271 * @method createUser |
| 272 * @param {string} email |
| 273 * @param {string} password |
| 274 */ |
| 275 createUser: function(email, password) { |
| 276 this.ref.createUser({email: email, password: password}, function(error)
{ |
| 277 if (!error) { |
| 278 this.fire('user-created'); |
| 279 } else { |
| 280 this.fire('error', error); |
| 281 } |
| 282 }.bind(this)); |
| 283 }, |
| 284 |
| 285 /** |
| 286 * Changes the password of a "password provider"-based user account. |
| 287 * |
| 288 * If the operation is successful, the `password-changed` event is fired. |
| 289 * |
| 290 * If the operation fails, the `error` event is fired, with `e.detail` |
| 291 * containing error information supplied from Firebase. |
| 292 * |
| 293 * @method changePassword |
| 294 * @param {string} email |
| 295 * @param {string} oldPassword |
| 296 * @param {string} newPassword |
| 297 */ |
| 298 changePassword: function(email, oldPassword, newPassword) { |
| 299 this.ref.changePassword({ |
| 300 email: email, |
| 301 oldPassword: oldPassword, |
| 302 newPassword: newPassword |
| 303 }, function(error) { |
| 304 if (!error) { |
| 305 this.fire('password-changed'); |
| 306 } else { |
| 307 this.fire('error', error); |
| 308 } |
| 309 }.bind(this)); |
| 310 }, |
| 311 |
| 312 /** |
| 313 * Sends a password reset email for a "password provider"-based user account
. |
| 314 * |
| 315 * If the operation is successful, the `password-reset` event is fired. |
| 316 * |
| 317 * If the operation fails, the `error` event is fired, with `e.detail` |
| 318 * containing error information supplied from Firebase. |
| 319 * |
| 320 * @method sendPasswordResetEmail |
| 321 * @param {string} email |
| 322 */ |
| 323 sendPasswordResetEmail: function(email) { |
| 324 this.ref.resetPassword({email: email}, function(error) { |
| 325 if (!error) { |
| 326 this.fire('password-reset'); |
| 327 } else { |
| 328 this.fire('error', error); |
| 329 } |
| 330 }.bind(this)); |
| 331 }, |
| 332 |
| 333 /** |
| 334 * Changes the email of a "password provider"-based user account. |
| 335 * |
| 336 * If the operation is successful, the `email-changed` event is fired. |
| 337 * |
| 338 * If the operation fails, the `error` event is fired, with `e.detail` |
| 339 * containing error information supplied from Firebase. |
| 340 * |
| 341 * @method changeEmail |
| 342 * @param {string} oldEmail |
| 343 * @param {string} newEmail |
| 344 * @param {string} Password |
| 345 */ |
| 346 changeEmail: function(oldEmail, newEmail, password) { |
| 347 this.ref.changeEmail({ |
| 348 oldEmail: oldEmail, |
| 349 newEmail: newEmail, |
| 350 password: password |
| 351 }, function(error) { |
| 352 if (!error) { |
| 353 this.fire('email-changed'); |
| 354 } else { |
| 355 this.fire('error', error); |
| 356 } |
| 357 }.bind(this)); |
| 358 }, |
| 359 |
| 360 /** |
| 361 * Removes a "password provider"-based user account. |
| 362 * |
| 363 * If the operation is successful, the `user-removed` event is fired. |
| 364 * |
| 365 * If the operation fails, the `error` event is fired, with `e.detail` |
| 366 * containing error information supplied from Firebase. |
| 367 * |
| 368 * @method removeUser |
| 369 * @param {string} email |
| 370 * @param {string} password |
| 371 */ |
| 372 removeUser: function(email, password) { |
| 373 this.ref.removeUser({email: email, password: password}, function(error, su
ccess) { |
| 374 if (!error) { |
| 375 this.fire('user-removed'); |
| 376 } else { |
| 377 this.fire('error', error); |
| 378 } |
| 379 }.bind(this)); |
| 380 } |
| 381 }); |
| 382 </script> |
| 383 |
| OLD | NEW |