Commit 4995603e by muhsin

added change password

parent 30081a1f
...@@ -72,9 +72,22 @@ ...@@ -72,9 +72,22 @@
<div class="row"> <div class="row">
<h6>Change Password</h6> <h6>Change Password</h6>
</div> </div>
<!-- <div class="row"> <div class="row">
<input class="" type="password" placeholder="Current Password" /> <input
</div> --> class=""
type="password"
minlength="6"
#currentPassword="ngModel"
[(ngModel)]="updatePassword.currentPassword"
name="currentPassword"
placeholder="Current Password"
/>
<div
*ngIf="currentPassword.errors && currentPassword.errors.minlength"
>
Password must be at least 6 characters
</div>
</div>
<div class="row"> <div class="row">
<input <input
class="" class=""
...@@ -83,6 +96,7 @@ ...@@ -83,6 +96,7 @@
type="password" type="password"
placeholder="New Password" placeholder="New Password"
minlength="6" minlength="6"
[required]="currentPassword ? true: false"
#password="ngModel" #password="ngModel"
/> />
<div *ngIf="password.errors && password.errors.minlength"> <div *ngIf="password.errors && password.errors.minlength">
...@@ -96,10 +110,11 @@ ...@@ -96,10 +110,11 @@
[(ngModel)]="updatePassword.confirmPassword" [(ngModel)]="updatePassword.confirmPassword"
name="confirmPassword" name="confirmPassword"
type="password" type="password"
[required]="currentPassword ? true: false"
placeholder="Confirm Password" placeholder="Confirm Password"
/> />
<div *ngIf=""> <div *ngIf="confirmPassword.errors">
Password should match {{confPasswordErr}}
</div> </div>
</div> </div>
</div> </div>
......
...@@ -17,6 +17,7 @@ export class ChangedetailsPage implements OnInit { ...@@ -17,6 +17,7 @@ export class ChangedetailsPage implements OnInit {
name: '', name: '',
phone: '' phone: ''
}; };
confPasswordErr: string;
updatePassword: UpdatePassword = { updatePassword: UpdatePassword = {
password: '', password: '',
...@@ -62,19 +63,28 @@ export class ChangedetailsPage implements OnInit { ...@@ -62,19 +63,28 @@ export class ChangedetailsPage implements OnInit {
console.log(form.value); console.log(form.value);
if (form.valid) { if (form.valid) {
// Change Password // Change Password
if (form.value.password) { if (form.value.currentPassword && form.value.password) {
if (form.value.password === form.value.confirmPassword) { if (
form.value.password &&
form.value.password === form.value.confirmPassword
) {
// this.register.updateData(form.value, this.userData.uid); // this.register.updateData(form.value, this.userData.uid);
// this.register.updatePassword(form.value.password); this.register.updatePassword(
console.log('Password Changed and profile updated'); form.value.currentPassword,
form.value.password
);
console.log('Password Changed and profile updated'), form.value;
} else { } else {
console.log('Password should match'); console.log('Password should match');
this.confPasswordErr = 'Password should match';
} }
} else { } else {
this.register.updateData(form.value, this.userData); // this.register.updateData(form.value, this.userData);
console.log('profile updated'); console.log('profile updated', form.value);
} }
console.log('valid form values', form.value); // console.log('valid form values', form.value);
} else {
console.log(form);
} }
} }
} }
...@@ -48,6 +48,7 @@ export class UpdateService { ...@@ -48,6 +48,7 @@ export class UpdateService {
// }); // });
} }
// Update User Data
updateData(userData: UpdateUserDetails, oldData: User) { updateData(userData: UpdateUserDetails, oldData: User) {
this.loader = true; this.loader = true;
this.type = 2; this.type = 2;
...@@ -85,4 +86,50 @@ export class UpdateService { ...@@ -85,4 +86,50 @@ export class UpdateService {
console.log(error); console.log(error);
}); });
} }
// Change user password
updatePassword(currentPassword: string, newPasswd: string) {
this.loader = true;
let currentUser = this.afAuth.auth.currentUser; // Gets current user
// Credential is required for re-authentication
const credential = auth.EmailAuthProvider.credential(
currentUser.email,
currentPassword
);
// Re-authenticate user
currentUser
.reauthenticateWithCredential(credential)
.then(() => {
currentUser
.updatePassword(newPasswd) // Update password
.then(success => {
console.log('password changed');
this.loader = false;
this.service
.showToast('Password Changed!', 'top', 'my-toast', 1000) //success toast
.then(res => {
this.router.navigateByUrl('profile');
this.loader = false;
});
})
.catch(error => {
console.log('something fishy');
this.service
.showToast('Something went wrong', 'top', 'my-toast', 1000) //failure toast
.then(res => {
this.loader = false;
});
});
})
.catch(error => {
console.log(error);
this.service
.showToast(error.message, 'top', 'my-error', 3000) //failure toast
.then(res => {
this.loader = false;
});
});
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment