Skip to content
This repository was archived by the owner on Mar 17, 2025. It is now read-only.

Commit b587e18

Browse files
committed
Merge branch 'master' into kato-extendFactory
2 parents 80a40b0 + 9fdffec commit b587e18

File tree

7 files changed

+276
-235
lines changed

7 files changed

+276
-235
lines changed

.jshintrc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
{
22
"predef": [
33
"angular",
4-
"Firebase",
5-
"FirebaseSimpleLogin"
4+
"Firebase"
65
],
76
"bitwise": true,
87
"browser": true,
98
"curly": true,
109
"forin": true,
1110
"indent": 2,
1211
"latedef": true,
13-
"maxlen": 115,
1412
"noempty": true,
1513
"nonbsp": true,
1614
"strict": true,
1715
"trailing": true,
1816
"undef": true,
1917
"unused": true
20-
}
18+
}

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,6 @@ tests, run `grunt test:unit`. To only run the end-to-end [Protractor](https://gi
9696
tests, run `grunt test:e2e`.
9797

9898
In addition to the automated test suite, there is an additional manual test suite that ensures that
99-
the `$firebaseSimpleLogin` service is working properly with the authentication providers. These tests
100-
can be run with `grunt test:manual`. Note that you must click "Close this window", login to Twitter,
99+
the `$firebaseUser` service is working properly with the authentication providers. These tests can
100+
be run with `grunt test:manual`. Note that you must click "Close this window", login to Twitter,
101101
etc. when prompted in order for these tests to complete successfully.

bower.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@
3131
],
3232
"dependencies": {
3333
"angular": "1.2.x || 1.3.x",
34-
"firebase": "1.0.x",
35-
"firebase-simple-login": "1.6.x"
34+
"firebase": "2.0.x"
3635
},
3736
"devDependencies": {
3837
"lodash": "~2.4.1",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"package.json"
3232
],
3333
"dependencies": {
34-
"firebase": "1.0.x"
34+
"firebase": "2.0.x"
3535
},
3636
"devDependencies": {
3737
"coveralls": "^2.11.1",

src/FirebaseAuth.js

Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
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

Comments
 (0)