Dear Stackoverflow community,
We are using Firebase Auth for our authentication. For this we are offering social sign in with google and facebook as well as signing in with email and password. The following code shows the registration page for users that want to sign in with email and password later.
import { Router } from '@angular/router';
import { Component, OnInit } from '@angular/core';
import { AngularFireAuth } from '@angular/fire/auth';
import { auth } from 'firebase/app';
@Component({
selector: 'app-register',
templateUrl: './register.component.html',
styleUrls: ['./register.component.css']
})
export class RegisterComponent implements OnInit {
public emailAddress="";
public firstName="";
public lastName="";
public password="";
constructor(public afAuth: AngularFireAuth, private router: Router) {
}
ngOnInit() {
}
reset() {
this.emailAddress=""
this.password=""
this.firstName=""
this.lastName=""
}
register() {
let emailAddress = this.emailAddress
let password = this.password
let firstName = this.firstName
let lastName = this.lastName
this.reset()
console.log(emailAddress)
console.log(password)
auth().createUserWithEmailAndPassword(emailAddress, password).catch(function(error) {
// Handle Errors here.
var errorCode = error.code;
console.log(errorCode)
var errorMessage = error.message;
console.log(errorMessage)
// ...
});
}
}
The data for email, password, firstName, lastName is coming from an input field that exchanges data via data binding with the component.
<input type="email" [(ngModel)]="emailAddress" required>
When I try to get the users display name its only working when the user signed in using the social provider. Thats kind of obvious because he didn't set it when registering with email and password. Now i wanted to ask you if someone knows how to store the firstName and lastName in firebase so the user.displayName can be shown.
<div *ngIf="afAuth.user | async as user; else notAuth">
Hello {{ user.displayName }}
</div>
displayName
of that user. You can use updateProfile(). As on approach, you could usethen()
ofcreateUserWithEmailAndPassword
to executeupdateProfile()
. – Alexander Staroselsky