Chrillewoodz
Chrillewoodz

Reputation: 28368

Getting the user ID of the created user

I'm using angularfire as backend in my angular app but I need help when it comes to extracting the user ID of the last account that has just been created.

Below is an image from the "login and auth" section in the database, this is where the user ID resides.

userid image

This is the function that creates a new user, using the email and password that the user enters via a registration form. (user is an object that comes from a validation function that gets run before this one).

$scope.registerNewUser = function(user) {

    var ref = new Firebase("https://pie-shop.firebaseio.com/");
        ref.createUser({
            email    : user.email,
            password : user.password
        }, function(error) {
            if (error === null) {
                $('#error-message').show().html('Success!').css({color: 'limegreen'});
            }
            else {
                $('#error-message').show()
                .html('Something went wrong when trying to create your account, please try again later')
                .css({color: 'red'});
            } 
        });
},
$scope.storeUserInfo = function(user, userID) {

    var newUser = new Firebase('https://pie-shop.firebaseio.com/' + userID);
}

So what I need to do is, preferably in the function(error) if statement, is get the user ID of the newly created account and call the storeUserInfo function with the user ID as an argument so that I can use it to store the other information that the user have entered.

By simply using $push to store the information in the database wouldn't allow me to match the user ID with the key to get the corresponding information since Firebase generates unique keys when pushing data into the database. (Or is there a clever way even by using push? Let me know if that's the case).

Upvotes: 0

Views: 684

Answers (2)

m1crdy
m1crdy

Reputation: 1401

In this tutorial (section: Authenticating users with a service) you gonna do this. You register a user and log him in instantly. The user-object is then stored in $scope.user. You are able to get the user id from firebase with $scope.user.userId (or console.log it to find the correct value). Hope it helps. By the way: Great tutorial for learning angular and firebase.

Upvotes: 0

New Dev
New Dev

Reputation: 49610

From Firebase docs on createUser:

onComplete Function

A callback function that will be called when the user account has been created. On failure, the first argument will be an Error object indicating the failure, with a machine-readable code attribute. On success, the first argument will be null, and the second argument will be an object containing attributes of the newly-created user, including the uid.

(emphasis mine)

So, do this:

var ref = new Firebase("https://pie-shop.firebaseio.com/");
ref.createUser({
    email    : user.email,
    password : user.password
}, function(error, user) {
    if (error === null) {
        console.log(user);
    }
    else {
        // deal with error
    } 
});

Upvotes: 3

Related Questions