I'm working on an app and I want to achieve this. When an user signing in I want to get his username and store in firebase. So I created this :
Signup(email: string, password: string, displayName: string) {
this.angFire.auth.createUser({
email: email,
password: password
}).catch(
(err) => {
console.log(err);
this.error = err;
})
...
And this works, now I need to save also displayName into current user, so I try to do this inside the same function (Singup()) :
let user = firebase.auth().currentUser;
user.updateProfile({
displayName: this.displayName,
photoURL: "https://example.com/jane-q-user/profile.jpg"
}).then(function() {
console.log("Nome utente inserito");
}, function(error) {
console.log("Errore durante inserimento utente");
});
But it gives me this :
error_handler.js:54 EXCEPTION: Error in ./SignupPage class SignupPage - inline template:70:4 caused by: Cannot read property 'updateProfile' of null
How to do it? I'm running on ionic 2 angular2, firebase/angulrfire
Doc ref :
Update a user's profile
You can update a user's basic profile information—the user's display name and profile photo URL—with the updateProfile method. For example:
var user = firebase.auth().currentUser;
user.updateProfile({ displayName: "Jane Q. User", photoURL: "https://example.com/jane-q-user/profile.jpg" }).then(function() {
// Update successful. }, function(error) { // An error happened. });
Here
If a user isn't signed in, CurrentUser returns null. Note: CurrentUser might also return null because the auth object has not finished initializing. If you use a listener to keep track of the user's sign-in status, you don't need to handle this case.
You can easily detect if the user is logged or not by executing: var user = firebase. auth().
My answer is way too late but just in case someone is interested, you could do it like this
constructor(
private afAuth: AngularFireAuth,
private router: Router
) {
//
}
async signupUser(email: string, password: string): Promise<any> {
const data = await this.afAuth.createUserWithEmailAndPassword(
email,
password
);
return this.updateUserData(data.user).then(() => {
this.router.navigate(["/"]);
});
}
private updateUserData({ uid, email, photoURL, displayName }: User) {
const userRef: AngularFirestoreDocument<User> = this.afs.doc(
`users/${uid}`
);
return userRef.set({ uid, email, photoURL, displayName }, { merge: true });
}
You can learn more here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With