Files
quartz-manager/quartz-manager-frontend/src/app/views/change-password/change-password.component.ts
fabio.formosa 7687def758 #12 added jest
2020-05-11 23:16:02 +02:00

65 lines
1.7 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { AuthService } from '../../services';
import { Router } from '@angular/router';
import { DisplayMessage } from '../../shared/models/display-message';
import { delay, mergeMap } from 'rxjs/operators';
@Component({
selector: 'app-change-password',
templateUrl: './change-password.component.html',
styleUrls: ['./change-password.component.scss']
})
export class ChangePasswordComponent implements OnInit {
form: FormGroup;
/**
* Boolean used in telling the UI
* that the form has been submitted
* and is awaiting a response
*/
submitted = false;
/**
* Diagnostic message from received
* form request error
*/
notification: DisplayMessage;
constructor(
private authService: AuthService,
private router: Router,
private formBuilder: FormBuilder
) {
}
ngOnInit() {
this.form = this.formBuilder.group({
oldPassword: ['', Validators.compose([Validators.required, Validators.minLength(3), Validators.maxLength(64)])],
newPassword: ['', Validators.compose([Validators.required, Validators.minLength(3), Validators.maxLength(32)])]
});
}
onSubmit() {
/**
* Innocent until proven guilty
*/
this.notification = undefined;
this.submitted = true;
this.authService.changePassword(this.form.value)
.pipe(delay(1000), mergeMap(() => this.authService.logout()))
.subscribe(() => {
this.router.navigate(['/login', { msgType: 'success', msgBody: 'Success! Please sign in with your new password.'}]);
}, error => {
this.submitted = false;
this.notification = { msgType: 'error', msgBody: 'Invalid old password.'};
});
}
}