|
| 1 | +/* istanbul ignore next */ |
| 2 | +(function() { |
| 3 | + 'use strict'; |
| 4 | + var FirebaseAuth; |
| 5 | + |
| 6 | + // Define a service which provides user authentication and management. |
| 7 | + angular.module('firebase').factory('$firebaseAuth', [ |
| 8 | + '$q', function($q) { |
| 9 | + // This factory returns an object containing the current authentication state of the client. |
| 10 | + // This service takes one argument: |
| 11 | + // |
| 12 | + // * `ref`: A Firebase reference. |
| 13 | + // |
| 14 | + // The returned object contains methods for authenticating clients, retrieving authentication |
| 15 | + // state, and managing users. |
| 16 | + return function(ref) { |
| 17 | + var auth = new FirebaseAuth($q, ref); |
| 18 | + return auth.construct(); |
| 19 | + }; |
| 20 | + } |
| 21 | + ]); |
| 22 | + |
| 23 | + FirebaseAuth = function($q, ref) { |
| 24 | + this._q = $q; |
| 25 | + |
| 26 | + if (typeof ref === 'string') { |
| 27 | + throw new Error('Please provide a Firebase reference instead of a URL when creating a `$firebaseAuth` object.'); |
| 28 | + } |
| 29 | + this._ref = ref; |
| 30 | + }; |
| 31 | + |
| 32 | + FirebaseAuth.prototype = { |
| 33 | + construct: function() { |
| 34 | + this._object = { |
| 35 | + // Authentication methods |
| 36 | + $authWithCustomToken: this.authWithCustomToken.bind(this), |
| 37 | + $authAnonymously: this.authAnonymously.bind(this), |
| 38 | + $authWithPassword: this.authWithPassword.bind(this), |
| 39 | + $authWithOAuthPopup: this.authWithOAuthPopup.bind(this), |
| 40 | + $authWithOAuthRedirect: this.authWithOAuthRedirect.bind(this), |
| 41 | + $authWithOAuthToken: this.authWithOAuthToken.bind(this), |
| 42 | + $unauth: this.unauth.bind(this), |
| 43 | + |
| 44 | + // Authentication state methods |
| 45 | + $onAuth: this.onAuth.bind(this), |
| 46 | + $getAuth: this.getAuth.bind(this), |
| 47 | + $requireAuth: this.requireAuth.bind(this), |
| 48 | + $waitForAuth: this.waitForAuth.bind(this), |
| 49 | + |
| 50 | + // User management methods |
| 51 | + $createUser: this.createUser.bind(this), |
| 52 | + $changePassword: this.changePassword.bind(this), |
| 53 | + $removeUser: this.removeUser.bind(this), |
| 54 | + $sendPasswordResetEmail: this.sendPasswordResetEmail.bind(this) |
| 55 | + }; |
| 56 | + |
| 57 | + return this._object; |
| 58 | + }, |
| 59 | + |
| 60 | + |
| 61 | + /********************/ |
| 62 | + /* Authentication */ |
| 63 | + /********************/ |
| 64 | + // Common login completion handler for all authentication methods. |
| 65 | + _onLoginHandler: function(deferred, error, authData) { |
| 66 | + if (error !== null) { |
| 67 | + deferred.reject(error); |
| 68 | + } else { |
| 69 | + deferred.resolve(authData); |
| 70 | + } |
| 71 | + }, |
| 72 | + |
| 73 | + // Authenticates the Firebase reference with a custom authentication token. |
| 74 | + authWithCustomToken: function(authToken) { |
| 75 | + var deferred = this._q.defer(); |
| 76 | + |
| 77 | + this._ref.authWithCustomToken(authToken, this._onLoginHandler.bind(this, deferred)); |
| 78 | + |
| 79 | + return deferred.promise; |
| 80 | + }, |
| 81 | + |
| 82 | + // Authenticates the Firebase reference anonymously. |
| 83 | + authAnonymously: function(options) { |
| 84 | + var deferred = this._q.defer(); |
| 85 | + |
| 86 | + this._ref.authAnonymously(this._onLoginHandler.bind(this, deferred), options); |
| 87 | + |
| 88 | + return deferred.promise; |
| 89 | + }, |
| 90 | + |
| 91 | + // Authenticates the Firebase reference with an email/password user. |
| 92 | + authWithPassword: function(credentials, options) { |
| 93 | + var deferred = this._q.defer(); |
| 94 | + |
| 95 | + this._ref.authWithPassword(credentials, this._onLoginHandler.bind(this, deferred), options); |
| 96 | + |
| 97 | + return deferred.promise; |
| 98 | + }, |
| 99 | + |
| 100 | + // Authenticates the Firebase reference with the OAuth popup flow. |
| 101 | + authWithOAuthPopup: function(provider, options) { |
| 102 | + var deferred = this._q.defer(); |
| 103 | + |
| 104 | + this._ref.authWithOAuthPopup(provider, this._onLoginHandler.bind(this, deferred), options); |
| 105 | + |
| 106 | + return deferred.promise; |
| 107 | + }, |
| 108 | + |
| 109 | + // Authenticates the Firebase reference with the OAuth redirect flow. |
| 110 | + authWithOAuthRedirect: function(provider, options) { |
| 111 | + var deferred = this._q.defer(); |
| 112 | + |
| 113 | + this._ref.authWithOAuthRedirect(provider, this._onLoginHandler.bind(this, deferred), options); |
| 114 | + |
| 115 | + return deferred.promise; |
| 116 | + }, |
| 117 | + |
| 118 | + // Authenticates the Firebase reference with an OAuth token. |
| 119 | + authWithOAuthToken: function(provider, credentials, options) { |
| 120 | + var deferred = this._q.defer(); |
| 121 | + |
| 122 | + this._ref.authWithOAuthToken(provider, credentials, this._onLoginHandler.bind(this, deferred), options); |
| 123 | + |
| 124 | + return deferred.promise; |
| 125 | + }, |
| 126 | + |
| 127 | + // Unauthenticates the Firebase reference. |
| 128 | + unauth: function() { |
| 129 | + if (this.getAuth() !== null) { |
| 130 | + this._ref.unauth(); |
| 131 | + } |
| 132 | + }, |
| 133 | + |
| 134 | + |
| 135 | + /**************************/ |
| 136 | + /* Authentication State */ |
| 137 | + /**************************/ |
| 138 | + // Asynchronously fires the provided callback with the current authentication data every time |
| 139 | + // the authentication data changes. It also fires as soon as the authentication data is |
| 140 | + // retrieved from the server. |
| 141 | + onAuth: function(callback) { |
| 142 | + var self = this; |
| 143 | + |
| 144 | + this._ref.onAuth(callback); |
| 145 | + |
| 146 | + // Return a method to detach the `onAuth()` callback. |
| 147 | + return function() { |
| 148 | + self._ref.offAuth(callback); |
| 149 | + }; |
| 150 | + }, |
| 151 | + |
| 152 | + // Synchronously retrieves the current authentication data. |
| 153 | + getAuth: function() { |
| 154 | + return this._ref.getAuth(); |
| 155 | + }, |
| 156 | + |
| 157 | + // Helper onAuth() callback method for the two router-related methods. |
| 158 | + _routerMethodOnAuthCallback: function(deferred, rejectIfAuthDataIsNull, authData) { |
| 159 | + if (authData !== null) { |
| 160 | + deferred.resolve(authData); |
| 161 | + } else if (rejectIfAuthDataIsNull) { |
| 162 | + deferred.reject("AUTH_REQUIRED"); |
| 163 | + } else { |
| 164 | + deferred.resolve(null); |
| 165 | + } |
| 166 | + |
| 167 | + // Turn off this onAuth() callback since we just needed to get the authentication data once. |
| 168 | + this._ref.offAuth(this._routerMethodOnAuthCallback); |
| 169 | + }, |
| 170 | + |
| 171 | + // Returns a promise which is resolved if the client is authenticated and rejects otherwise. |
| 172 | + // This can be used to require that a route has a logged in user. |
| 173 | + requireAuth: function() { |
| 174 | + var deferred = this._q.defer(); |
| 175 | + |
| 176 | + this._ref.onAuth(this._routerMethodOnAuthCallback.bind(this, deferred, /* rejectIfAuthDataIsNull */ true)); |
| 177 | + |
| 178 | + return deferred.promise; |
| 179 | + }, |
| 180 | + |
| 181 | + // Returns a promise which is resolved with the client's current authenticated data. This can |
| 182 | + // be used in a route's resolve() method to grab the current authentication data. |
| 183 | + waitForAuth: function() { |
| 184 | + var deferred = this._q.defer(); |
| 185 | + |
| 186 | + this._ref.onAuth(this._routerMethodOnAuthCallback.bind(this, deferred, /* rejectIfAuthDataIsNull */ false)); |
| 187 | + |
| 188 | + return deferred.promise; |
| 189 | + }, |
| 190 | + |
| 191 | + |
| 192 | + /*********************/ |
| 193 | + /* User Management */ |
| 194 | + /*********************/ |
| 195 | + // Creates a new email/password user. Note that this function only creates the user, if you |
| 196 | + // wish to log in as the newly created user, call $authWithPassword() after the promise for |
| 197 | + // this method has been resolved. |
| 198 | + createUser: function(email, password) { |
| 199 | + var deferred = this._q.defer(); |
| 200 | + |
| 201 | + this._ref.createUser({ |
| 202 | + email: email, |
| 203 | + password: password |
| 204 | + }, function(error) { |
| 205 | + if (error !== null) { |
| 206 | + deferred.reject(error); |
| 207 | + } else { |
| 208 | + deferred.resolve(); |
| 209 | + } |
| 210 | + }); |
| 211 | + |
| 212 | + return deferred.promise; |
| 213 | + }, |
| 214 | + |
| 215 | + // Changes the password for an email/password user. |
| 216 | + changePassword: function(email, oldPassword, newPassword) { |
| 217 | + var deferred = this._q.defer(); |
| 218 | + |
| 219 | + this._ref.changePassword({ |
| 220 | + email: email, |
| 221 | + oldPassword: oldPassword, |
| 222 | + newPassword: newPassword |
| 223 | + }, function(error) { |
| 224 | + if (error !== null) { |
| 225 | + deferred.reject(error); |
| 226 | + } else { |
| 227 | + deferred.resolve(); |
| 228 | + } |
| 229 | + } |
| 230 | + ); |
| 231 | + |
| 232 | + return deferred.promise; |
| 233 | + }, |
| 234 | + |
| 235 | + // Removes an email/password user. |
| 236 | + removeUser: function(email, password) { |
| 237 | + var deferred = this._q.defer(); |
| 238 | + |
| 239 | + this._ref.removeUser({ |
| 240 | + email: email, |
| 241 | + password: password |
| 242 | + }, function(error) { |
| 243 | + if (error !== null) { |
| 244 | + deferred.reject(error); |
| 245 | + } else { |
| 246 | + deferred.resolve(); |
| 247 | + } |
| 248 | + }); |
| 249 | + |
| 250 | + return deferred.promise; |
| 251 | + }, |
| 252 | + |
| 253 | + // Sends a password reset email to an email/password user. |
| 254 | + sendPasswordResetEmail: function(email) { |
| 255 | + var deferred = this._q.defer(); |
| 256 | + |
| 257 | + this._ref.resetPassword({ |
| 258 | + email: email |
| 259 | + }, function(error) { |
| 260 | + if (error !== null) { |
| 261 | + deferred.reject(error); |
| 262 | + } else { |
| 263 | + deferred.resolve(); |
| 264 | + } |
| 265 | + }); |
| 266 | + |
| 267 | + return deferred.promise; |
| 268 | + } |
| 269 | + }; |
| 270 | +})(); |
0 commit comments