Angularfire Email/PW Auth with User db creation – Angular Factory
You can authenticate a user with the new Firebase email and password auth, but you are too lazy to dig through the new codebase to figure out how to create a user in the database on the returned promise? I was too and couldn’t find the answer until I stopped, regrouped, grabbed a beer, and read the new docs.
This assumes your app is using the using firebase 3.0.4+ and angularfire 2.0.2+ with the new config apiKey, authDomain, databaseURL:
myApp.factory('Authentication', ['$rootScope', '$firebaseObject', '$firebaseAuth', function($rootScope, $firebaseObject, $firebaseAuth){
var ref = firebase.database().ref(); var auth = $firebaseAuth();
return { login: function(user) { $rootScope.message = "Welcome " + $scope.user.email; }, //login
register: function(user) { var email = user.email, password = user.password;
auth.$createUserWithEmailAndPassword(email, password).then(function(regUser) {
var regRef = ref.child('users/' + regUser.uid).set({ date: firebase.database.ServerValue.TIMESTAMP, regUser: regUser.uid, firstname: user.firstname, lastname: user.lastname, email: user.email });
$rootScope.message = "Hi " + user.firstname + ", Thanks for registering"; }).catch(function(error) { $rootScope.message = error.message; }); } //register };
}]);
If you want the entire setup, message me. If you have a better way to structure this, have enhancements, please share it. It is all about progression.















