简体   繁体   中英

Angular-Firebase How to store addiotional fields during signup in angular firebase?

This is my controller for signup, I have successfully signup but I don't know any way to store some additional fields like name, occupation, etc. How can I do that?

controller

app.controller('signupCtrl', function ($http, $scope, toaster, $location, $firebaseAuth) {
var ref = firebase.database().ref();
auth = $firebaseAuth(firebase.auth());

$scope.registerData = {};
$scope.register = function () {

    auth.$createUserWithEmailAndPassword($scope.registerData.email, $scope.registerData.password)
        .then(function (data) {
            $scope.message = "Welcome " + data.uid + ", thanks for registering!";
            console.log(data.uid);
        }).catch(function (error) {
                $scope.message = error.message;
                console.log($scope.message);
            });

    };
});

Do I have to do something in then function or is there already something to handle this ?

If you are looking to store your user details in Firebase, use Firebase database instead. Try this:

{
  "userList": {
    "JRHTHaIsjNPLXOQivY": {
      "userName": "userA",
      "occupation": "programmer"
    },
    "JRHTHaKuTFIhnj02kE": {
      "userName": "userB",
      "occupation": "clerk"
    }
  }
}

You have to create a function that saves your data to the database after you successfully created the account. Your controller should be like this:

auth.$createUserWithEmailAndPassword($scope.registerData.email, $scope.registerData.password)
    .then(function (data) {
        writeUserData(userId, name, useroccupation);
        $scope.message = "Welcome " + data.uid + ", thanks for registering!";
        console.log(data.uid);
    }).catch(function (error) {
        $scope.message = error.message;
        console.log($scope.message);
    });

};

function writeUserData(userId, name, useroccupation) {
    firebase.database().ref('users/' + userId).set({
        username: name,
        occupation: useroccupation
    });
}

You can see that the funtion writeUserData is called after the user is successfully created.

Make sure that you save all the details inside userID. Hope it helps. :D

FINALLY i figure it out myself.

app.controller('signupCtrl', function ($http, $scope, toaster, $location, $firebaseAuth) {
var ref = firebase.database().ref();
auth = $firebaseAuth(firebase.auth());
var database = firebase.database();
$scope.registerData = {};
$scope.register = function () {
    auth.$createUserWithEmailAndPassword($scope.registerData.email, $scope.registerData.password)
        .then(function (data) {
            $scope.message = "Welcome " + data.uid + ", thanks for registering!";
            console.log(data.uid);
            firebase.database().ref('users/' + data.uid).set({username: $scope.registerData.username, role: $scope.registerData.role,});
        }).catch(function (error) {
    $scope.message = error.message;
    console.log($scope.message);
});

};

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM