Hello! How i can change password with angularfire2?
Thanks
Hi @Leanvitale! Please follow our issue template for any future questions. This repo is dedicated to troubleshooting library issues, questions like this are best suited for stackoverflow. However, in your case you can use the injected auth service for AngularFire.
constructor(afAuth: AngularFireAuth) {
const auth = afAuth.auth;
const user = auth().currentUser;
const newPassword = getASecureRandomPassword();
user.updatePassword(newPassword).then(function() {
// Update successful.
}).catch(function(error) {
// An error happened.
});
}
I came across an behaviour where you might receive an error, when the user has not signed in recently and trys to update the password.
ERROR TypeError: Cannot read property 'updatePassword' of null
To fix this make sure to check currentUser against null, reauth if needed. Further read firebase.updatePassword from firebase docs.
UPDATE REAUTH EXAMPLE
const user = this.afAuth.auth.currentUser;
const credentials = firebase.auth.EmailAuthProvider.credential(user.email, 'password');
user.reauthenticateWithCredential(credentials)
.then(() => console.log('reauth ok'));
May be my answer will be helpful, Password can be changed by firebase-admin using cloud function you will just have to pass email and new password from the client-side(Angular, ionic, ...)
Cloud function:
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
const cors = require('cors')({ origin: true });
export const resetUserPassword = functions.https.onRequest( async (req, res) => {
return cors( req, res, async () => {
const email = req.body.data.email;
const password = req.body.data.password;
admin.auth().getUserByEmail(email)
.then((getUserRecord) => {
admin.auth().updateUser(getUserRecord.uid, {
email,
password
})
.then(function(userRecord) {
res.send({
status: 200,
data: userRecord
})
})
.catch(function(error) {
res.send({
status: 500,
data: error
})
});
})
.catch((error) => {
console.log('Error fetching user data:', error);
});
})
});
Client side code(IONIC):
import { AngularFireFunctions } from '@angular/fire/functions';
export class AuthService {
constructor(
private functions: AngularFireFunctions
) {}
resetUserPassword() {
const userNewCredential = {email: 'user-email', password: 'your-new-password'};
this.functions.httpsCallable('resetUserPassword')
(userNewCredential).toPromise().then((updatedUser) => {
console.log(updatedUser);
}).catch((error) => console.log(error));
}
Most helpful comment
Hi @Leanvitale! Please follow our issue template for any future questions. This repo is dedicated to troubleshooting library issues, questions like this are best suited for stackoverflow. However, in your case you can use the injected auth service for AngularFire.