import { Component, OnInit } from '@angular/core' import { FormControl, FormGroup, Validators } from '@angular/forms' import { ActivatedRoute, Router } from '@angular/router' import { UikitAuthService } from '@bit/dapi.lib-uikit-auth.uikit-auth/dist/uikit-auth' import { PasswordService } from 'src/app/services/password/password.service' import { environment } from '../../../environments/environment' @Component({ selector: 'app-confirm-password', templateUrl: './confirm-password.component.html', styleUrls: ['./confirm-password.component.scss'], }) export class ConfirmPasswordComponent implements OnInit { errorMessage = false recoverForm = new FormGroup({ user: new FormControl('', [ Validators.required, Validators.pattern('^[^\\s@]+@[^\\s@]+\\.[^\\s@]{2,}$'), ]), password: new FormControl('', [ Validators.required, Validators.pattern( '^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\\s).{8,15}$', ), ]), code: new FormControl('', Validators.required), }) constructor( private router: Router, private route: ActivatedRoute, private passwordService: PasswordService, private snackBarService: UikitAuthService, ) {} ngOnInit() { this.route.queryParams.subscribe((params) => { if ('code' in params && params.code) { this.recoverForm.patchValue({ code: params.code, }) } }) } confirmPassword() { this.passwordService .confirmResetPassword( this.recoverForm.value.user, this.recoverForm.value.password, this.recoverForm.value.code, ) .then((response) => { this.snackBarService.openSnackBar('Password updated') this.router.navigate([''], { queryParams: { redirect_uri: environment.adminUrl }, }) }) .catch((err) => { this.snackBarService.openSnackBar( 'error' in err ? err.error : 'Error restoring the password.', ) }) } }