Search code examples
databaseangularregistrationangularfire2

AngularFire2 How to CreateUser ( name, surname, email, password )


I know how to create user with Email and Password, but how can i create him with also name and surname? This is my code form email and password

registerUser(formData) {
    if(formData.valid) {
        this.af.auth.createUser({
            email: formData.value.email,
            password: formData.value.password,
            }).then((success) => {
                this.routing.navigateByUrl('/login');
            }).catch((error) => {
                console.log(error);
            })
    }
}

I cannot just write

registerUser(formData) {
    if(formData.valid) {
        this.af.auth.createUser({
            email: formData.value.email,
            password: formData.value.password,
            name: formData.value.name, // this is error :(
            surname: formData.value.surname, // this is error :(
            }).then((success) => {
                this.routing.navigateByUrl('/login');
            }).catch((error) => {
                console.log(error);
            })
    }
}

Any ideas?


Solution

  • 'this.af.auth.createUser' only accepts an email and password. Also, you cannot have a custom property (name or surname) on Firebase's built in user profile.

    The only available properties are:

    • displayName
    • email
    • photoURL
    • providerId
    • uid

    So you could definitely store it in 'displayName' if you wanted, otherwise you're going to have to use the Firebase Realtime Database to store additional user data. I believe to set 'displayName' you'll have to have the person signed in.

    To use displayName you can do the following (once you have the signed in user):

    user.updateProfile({
      displayName: "Name here"
    }).then(() => {
      // Update successful
    }
    

    To use the Firebase Realtime Database:

    registerUser(formData) {
        if(formData.valid) {
            this.af.auth.createUser({
                // Create user
                email: formData.value.email,
                password: formData.value.password
            }).then((user) => {
                // User created now create the database user
                return this.af.database.object(`/users/${user.uid}`).update({
                    name: formData.value.name,
                    surname: formData.value.surname
                });
            }).then(() => {
                // Success
                this.routing.navigateByUrl('/login');
            }).catch((error) => {
                // Error
                console.log(error);
            });
        }
    }
    

    (Not had chance to test the above code).