Reputation: 2429
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 ?
Upvotes: 3
Views: 340
Reputation: 1351
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
Upvotes: 4
Reputation: 2429
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);
});
};
});
Upvotes: 2