import { Component, OnInit } from '@angular/core' import { FormControl, FormGroup, Validators } from '@angular/forms' import { 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-first-confirm-password', templateUrl: './first-confirm-password.component.html', styleUrls: ['./first-confirm-password.component.scss'], }) export class FirstConfirmPasswordComponent 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}$', ), ]), confirmPassword: new FormControl('', [Validators.required]), oldPassword: new FormControl('', [Validators.required]), }) constructor( private router: Router, private passwordService: PasswordService, private snackBarService: UikitAuthService, ) { const confirmParams = this.router.getCurrentNavigation().extras.state this.recoverForm.setValue({ password: '', confirmPassword: '', user: confirmParams.user, oldPassword: confirmParams.oldPassword, }) } ngOnInit() {} /** * Confirm password by first time * */ confirmPassword() { this.passwordService .confirmFirstPassword( this.recoverForm.get('user').value, this.recoverForm.get('oldPassword').value, this.recoverForm.get('password').value, ) .then((response: string) => { this.snackBarService.openSnackBar( 'Your password has been updated, please login', ) this.router.navigate([''], { queryParams: { redirect_uri: environment.adminUrl }, }) }) .catch((err) => { console.error(err) // prettier-ignore this.snackBarService.openSnackBar('An error ocrurred, your password wasn\'t updated') }) } }