Skip to content
This repository was archived by the owner on Aug 25, 2018. It is now read-only.

Commit 2db3f70

Browse files
committed
Simplified Auth class and cleaned up old 1.x code
1 parent b8e0a17 commit 2db3f70

File tree

5 files changed

+143
-128
lines changed

5 files changed

+143
-128
lines changed

app/js/auth.js

Lines changed: 93 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,113 +1,93 @@
1-
(function(angular) {
2-
angular.module('firebase.auth', ['firebase', 'firebase.utils'])
3-
4-
// a simple wrapper on simpleLogin.getUser() that rejects the promise
5-
// if the user does not exists (i.e. makes user required)
6-
.factory('requireUser', ['Auth', '$q', function(Auth, $q) {
7-
return function() {
8-
return Auth.getUser().then(function (user) {
9-
return user ? user : $q.reject({ authRequired: true });
10-
});
11-
}
12-
}])
13-
14-
.factory('Auth', ['$firebaseAuth', 'fbutil', function($firebaseAuth, fbutil) {
15-
var auth = $firebaseAuth(fbutil.ref());
16-
var listeners = [];
17-
18-
function statusChange() {
19-
fns.user = auth.$getAuth();
20-
angular.forEach(listeners, function(fn) {
21-
fn(fns.user);
22-
});
23-
}
24-
25-
var fns = {
26-
user: null,
27-
28-
getUser: function() {
29-
return auth.$waitForAuth();
30-
},
31-
32-
/**
33-
* @param {string} email
34-
* @param {string} pass
35-
* @returns {*}
36-
*/
37-
login: function(email, pass) {
38-
return auth.$authWithPassword({
39-
email: email,
40-
password: pass
41-
}, {rememberMe: true});
42-
},
43-
44-
logout: function() {
45-
auth.$unauth();
46-
},
47-
48-
createAccount: function(email, pass, name) {
49-
return auth.$createUser({email: email, password: pass})
50-
.then(function() {
51-
// authenticate so we have permission to write to Firebase
52-
return fns.login(email, pass);
53-
})
54-
.then(function(user) {
55-
var ref = fbutil.ref('users', user.uid);
56-
return fbutil.handler(function(cb) {
57-
ref.set({email: email, name: name||firstPartOfEmail(email)}, cb);
58-
});
59-
});
60-
},
61-
62-
changePassword: function(email, oldpass, newpass) {
63-
return auth.$changePassword({email: email, oldPassword: oldpass, newPassword: newpass});
64-
},
65-
66-
changeEmail: function(password, oldEmail, newEmail) {
67-
return auth.$changeEmail({password: password, oldEmail: oldEmail, newEmail: newEmail}, this)
68-
.then(function() {
69-
return fbutil.handler(function(cb) {
70-
var ref = fbutil.ref('users', fns.user.uid, 'email');
71-
ref.set(newEmail, cb);
72-
});
73-
});
74-
},
75-
76-
removeUser: function(email, pass) {
77-
return auth.$removeUser({email: email, password: pass});
78-
},
79-
80-
watch: function(cb, $scope) {
81-
fns.getUser().then(function(user) {
82-
cb(user);
83-
});
84-
listeners.push(cb);
85-
var unbind = function() {
86-
var i = listeners.indexOf(cb);
87-
if( i > -1 ) { listeners.splice(i, 1); }
88-
};
89-
if( $scope ) {
90-
$scope.$on('$destroy', unbind);
91-
}
92-
return unbind;
93-
}
94-
};
95-
96-
auth.$onAuth(statusChange);
97-
statusChange();
98-
99-
return fns;
100-
}]);
101-
102-
103-
function firstPartOfEmail(email) {
104-
return ucfirst(email.substr(0, email.indexOf('@'))||'');
105-
}
106-
107-
function ucfirst (str) {
108-
// inspired by: http://kevin.vanzonneveld.net
109-
str += '';
110-
var f = str.charAt(0).toUpperCase();
111-
return f + str.substr(1);
112-
}
113-
})(angular);
1+
angular.module('firebase.auth', ['firebase', 'firebase.utils'])
2+
.factory('Auth', ['$firebaseAuth', 'fbutil', function($firebaseAuth, fbutil) {
3+
return $firebaseAuth(fbutil.ref());
4+
}]);
5+
6+
// var fns = {
7+
// user: null,
8+
//
9+
// getUser: function() {
10+
// return auth.$waitForAuth();
11+
// },
12+
//
13+
// /**
14+
// * @param {string} email
15+
// * @param {string} pass
16+
// * @returns {*}
17+
// */
18+
// login: function(email, pass) {
19+
// return auth.$authWithPassword({
20+
// email: email,
21+
// password: pass
22+
// }, {rememberMe: true});
23+
// },
24+
//
25+
// logout: function() {
26+
// auth.$unauth();
27+
// },
28+
//
29+
// createAccount: function(email, pass, name) {
30+
// return auth.$createUser({email: email, password: pass})
31+
// .then(function() {
32+
// // authenticate so we have permission to write to Firebase
33+
// return fns.login(email, pass);
34+
// })
35+
// .then(function(user) {
36+
// var ref = fbutil.ref('users', user.uid);
37+
// return fbutil.handler(function(cb) {
38+
// ref.set({email: email, name: name||firstPartOfEmail(email)}, cb);
39+
// });
40+
// });
41+
// },
42+
//
43+
// changePassword: function(email, oldpass, newpass) {
44+
// return auth.$changePassword({email: email, oldPassword: oldpass, newPassword: newpass});
45+
// },
46+
//
47+
// changeEmail: function(password, oldEmail, newEmail) {
48+
// return auth.$changeEmail({password: password, oldEmail: oldEmail, newEmail: newEmail}, this)
49+
// .then(function() {
50+
// return fbutil.handler(function(cb) {
51+
// var ref = fbutil.ref('users', fns.user.uid, 'email');
52+
// ref.set(newEmail, cb);
53+
// });
54+
// });
55+
// },
56+
//
57+
// removeUser: function(email, pass) {
58+
// return auth.$removeUser({email: email, password: pass});
59+
// },
60+
//
61+
// watch: function(cb, $scope) {
62+
// fns.getUser().then(function(user) {
63+
// cb(user);
64+
// });
65+
// listeners.push(cb);
66+
// var unbind = function() {
67+
// var i = listeners.indexOf(cb);
68+
// if( i > -1 ) { listeners.splice(i, 1); }
69+
// };
70+
// if( $scope ) {
71+
// $scope.$on('$destroy', unbind);
72+
// }
73+
// return unbind;
74+
// }
75+
// };
76+
//
77+
// auth.$onAuth(statusChange);
78+
// statusChange();
79+
//
80+
// return fns;
81+
// }]);
82+
//
83+
//
84+
//function firstPartOfEmail(email) {
85+
// return ucfirst(email.substr(0, email.indexOf('@'))||'');
86+
//}
87+
//
88+
//function ucfirst (str) {
89+
// // inspired by: http://kevin.vanzonneveld.net
90+
// str += '';
91+
// var f = str.charAt(0).toUpperCase();
92+
// return f + str.substr(1);
93+
//}

app/js/controllers.js

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ angular.module('myApp.controllers', ['firebase.utils', 'firebase.auth'])
2626

2727
$scope.login = function(email, pass) {
2828
$scope.err = null;
29-
Auth.login(email, pass)
29+
Auth.$authWithPassword({ email: email, password: pass }, {rememberMe: true})
3030
.then(function(/* user */) {
3131
$location.path('/account');
3232
}, function(err) {
@@ -37,8 +37,23 @@ angular.module('myApp.controllers', ['firebase.utils', 'firebase.auth'])
3737
$scope.createAccount = function() {
3838
$scope.err = null;
3939
if( assertValidAccountProps() ) {
40-
Auth.createAccount($scope.email, $scope.pass)
40+
var email = $scope.email;
41+
var pass = $scope.pass;
42+
// create user credentials in Firebase auth system
43+
Auth.auth.$createUser({email: email, password: pass})
44+
.then(function() {
45+
// authenticate so we have permission to write to Firebase
46+
return $scope.login(email, pass);
47+
})
48+
.then(function(user) {
49+
// create a user profile in our data store
50+
var ref = fbutil.ref('users', user.uid);
51+
return fbutil.handler(function(cb) {
52+
ref.set({email: email, name: name||firstPartOfEmail(email)}, cb);
53+
});
54+
})
4155
.then(function(/* user */) {
56+
// redirect to the account page
4257
$location.path('/account');
4358
}, function(err) {
4459
$scope.err = errMessage(err);
@@ -62,6 +77,17 @@ angular.module('myApp.controllers', ['firebase.utils', 'firebase.auth'])
6277
function errMessage(err) {
6378
return angular.isObject(err) && err.code? err.code : err + '';
6479
}
80+
81+
function firstPartOfEmail(email) {
82+
return ucfirst(email.substr(0, email.indexOf('@'))||'');
83+
}
84+
85+
function ucfirst (str) {
86+
// inspired by: http://kevin.vanzonneveld.net
87+
str += '';
88+
var f = str.charAt(0).toUpperCase();
89+
return f + str.substr(1);
90+
}
6591
}])
6692

6793
.controller('AccountCtrl', ['$scope', 'Auth', 'fbutil', 'user', '$location', '$firebaseObject',
@@ -75,7 +101,7 @@ angular.module('myApp.controllers', ['firebase.utils', 'firebase.auth'])
75101
$scope.logout = function() {
76102
if( unbind ) { unbind(); }
77103
profile.$destroy();
78-
Auth.logout();
104+
Auth.$unauth();
79105
$location.path('/login');
80106
};
81107

@@ -88,7 +114,7 @@ angular.module('myApp.controllers', ['firebase.utils', 'firebase.auth'])
88114
$scope.err = 'New pass and confirm do not match';
89115
}
90116
else {
91-
Auth.changePassword(profile.email, pass, newPass)
117+
Auth.$changePassword({email: profile.email, oldPassword: pass, newPassword: newPass})
92118
.then(function() {
93119
$scope.msg = 'Password changed';
94120
}, function(err) {
@@ -102,7 +128,13 @@ angular.module('myApp.controllers', ['firebase.utils', 'firebase.auth'])
102128
$scope.changeEmail = function(pass, newEmail) {
103129
resetMessages();
104130
var oldEmail = profile.email;
105-
Auth.changeEmail(pass, oldEmail, newEmail)
131+
Auth.$changeEmail({oldEmail: oldEmail, newEmail: newEmail, password: pass})
132+
.then(function() {
133+
// store the new email address in the user's profile
134+
return fbutil.handle(function(done) {
135+
fbutil.ref('users', user.uid, 'email').set(newEmail, done);
136+
});
137+
})
106138
.then(function() {
107139
$scope.emailmsg = 'Email changed';
108140
}, function(err) {

app/js/decorators.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ angular.module('myApp.decorators', ['firebase.utils', 'firebase.auth'])
1515
// make a copy of the old directive
1616
var _compile = directive.compile;
1717
directive.compile = function(element, attr) {
18-
Auth.getUser().then(function() {
18+
Auth.$waitForAuth().then(function() {
1919
// after auth, run the original ng-cloak directive
2020
_compile.call(directive, element, attr);
2121
});

app/js/directives.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ angular.module('myApp.directives', ['firebase.auth'])
1616
*/
1717
.directive('ngShowAuth', ['Auth', '$timeout', function (Auth, $timeout) {
1818
var isLoggedIn;
19-
Auth.watch(function(user) {
19+
Auth.$onAuth(function(user) {
2020
isLoggedIn = !!user;
2121
});
2222

@@ -34,7 +34,8 @@ angular.module('myApp.directives', ['firebase.auth'])
3434
}
3535

3636
update();
37-
Auth.watch(update, scope);
37+
var dispose = Auth.$onAuth(update);
38+
scope.$on('$destroy', dispose);
3839
}
3940
};
4041
}])
@@ -44,7 +45,7 @@ angular.module('myApp.directives', ['firebase.auth'])
4445
*/
4546
.directive('ngHideAuth', ['Auth', '$timeout', function (Auth, $timeout) {
4647
var isLoggedIn;
47-
Auth.watch(function(user) {
48+
Auth.$onAuth(function(user) {
4849
isLoggedIn = !!user;
4950
});
5051

@@ -62,7 +63,8 @@ angular.module('myApp.directives', ['firebase.auth'])
6263
}
6364

6465
update();
65-
Auth.watch(update, scope);
66+
var dispose = Auth.$onAuth(update);
67+
scope.$on('$destroy', dispose);
6668
}
6769
};
6870
}]);

app/js/routes.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ angular.module('myApp.routes', ['ngRoute', 'firebase.auth'])
1212
// in the controller, but this makes things cleaner (controller doesn't need to worry
1313
// about auth status or timing of displaying its UI components)
1414
user: ['Auth', function(Auth) {
15-
return Auth.getUser();
15+
return Auth.$waitForAuth();
1616
}]
1717
}
1818
},
@@ -36,7 +36,8 @@ angular.module('myApp.routes', ['ngRoute', 'firebase.auth'])
3636

3737
/**
3838
* Adds a special `whenAuthenticated` method onto $routeProvider. This special method,
39-
* when called, invokes the requireUser() service (see Auth.js).
39+
* when called, waits for auth status to be resolved asynchronously, and then fails/redirects
40+
* if the user is not properly authenticated.
4041
*
4142
* The promise either resolves to the authenticated user object and makes it available to
4243
* dependency injection (see AuthCtrl), or rejects the promise if user is not logged in,
@@ -49,8 +50,8 @@ angular.module('myApp.routes', ['ngRoute', 'firebase.auth'])
4950
// to hack it directly onto the $routeProvider object
5051
$routeProvider.whenAuthenticated = function(path, route) {
5152
route.resolve = route.resolve || {};
52-
route.resolve.user = ['requireUser', function(requireUser) {
53-
return requireUser();
53+
route.resolve.user = ['Auth', function(Auth) {
54+
return Auth.$requireAuth();
5455
}];
5556
$routeProvider.when(path, route);
5657
}
@@ -84,7 +85,7 @@ angular.module('myApp.routes', ['ngRoute', 'firebase.auth'])
8485
.run(['$rootScope', '$location', 'Auth', 'ROUTES', 'loginRedirectPath',
8586
function($rootScope, $location, Auth, ROUTES, loginRedirectPath) {
8687
// watch for login status changes and redirect if appropriate
87-
Auth.watch(check, $rootScope);
88+
Auth.$onAuth(check);
8889

8990
// some of our routes may reject resolve promises with the special {authRequired: true} error
9091
// this redirects to the login page whenever that is encountered

0 commit comments

Comments
 (0)