import { Component, OnInit, ViewChild } from "@angular/core"; import { ActivatedRoute } from "@angular/router"; import { FormGroup, FormBuilder, Validators } from "@angular/forms"; import { UserService } from "@app/services/user.service"; import Swal from "sweetalert2"; import { ValidatorComponent, PasswordStrengthValidator, } from "../plugins/validator/validator.component"; @Component({ selector: "app-new-password", templateUrl: "./new-password.component.html", styleUrls: ["./new-password.component.scss"], }) export class NewPasswordComponent implements OnInit { token: string; validToken: boolean; activateForm: FormGroup; errorMessage: string; userActivated: boolean; successActivation: boolean; activateMessage: string; invalidToken: boolean; submitted: boolean = false; showDetails: boolean; newPass: boolean; userData: any; sendOtherToken: boolean; constructor( private formBuilder: FormBuilder, private route: ActivatedRoute, private userService: UserService ) { this.route.queryParams.subscribe((params) => { this.token = params["token"]; }); } ngOnInit() { Swal.fire({ allowOutsideClick: false, type: "info", text: "Espere por favor...", }); Swal.showLoading(); if (this.token !== null) { this.userService .validateUserToken({ token: this.token, process: "pwd_recovery", }) .subscribe( (res) => { Swal.close(); this.sendOtherToken = false; this.userData = res["data"].user; this.newPass = true; this.validToken = true; this.activateForm = this.formBuilder.group( { // Load information password: [ "", [ Validators.required, Validators.minLength(8), PasswordStrengthValidator, ], ], confirm_password: ["", Validators.required], }, { validator: ValidatorComponent("password", "confirm_password"), } ); }, (err) => { Swal.close(); this.newPass = false; this.userActivated = true; this.invalidToken = true; this.errorMessage = err.message; this.sendOtherToken = true; } ); } else { this.invalidToken = true; this.errorMessage = "No existe el token"; } } getNewToken() { Swal.fire({ allowOutsideClick: false, type: "info", text: "Espere por favor...", }); Swal.showLoading(); this.userService .sendAnotherToken({ token: this.token, }) .subscribe( (res) => { Swal.fire({ type: "success", title: "Exito", text: "Se ha enviado otro correo para recuperación de contraseña", }).then((result) => { this.successActivation = true; this.validToken = false; this.invalidToken = false; window.location.href = "#/investment-proposals"; }); }, (err) => { Swal.close(); this.validToken = false; this.errorMessage = err.message; } ); } get f() { return this.activateForm.controls; } newPassword() { this.submitted = true; // stop here if form is invalid if (this.activateForm.invalid) { return; } Swal.fire({ allowOutsideClick: false, type: "info", text: "Espere por favor...", }); Swal.showLoading(); this.userService .changePassword({ password: this.f.password.value, confirm_password: this.f.confirm_password.value, email: this.userData.email, first_name: this.userData.first_name, last_name: this.userData.last_name, phone_number: this.userData.phone_number, }) .subscribe( (res) => { Swal.fire({ type: "success", title: "Exito", text: "Su clave ha sido actualizada exitosamente", }).then((result) => { this.successActivation = true; this.validToken = false; window.location.href = "#/investment-proposals"; }); }, (err) => { Swal.fire({ type: "error", title: "Error en el servidor", text: err.message, }); this.validToken = false; } ); // } } /* Swal.fire({ allowOutsideClick: false, type: 'info', text: 'Espere por favor...' }); Swal.showLoading(); this.authService.login( { email: this.f.email.value, password: this.f.password.value } ) .subscribe(success => { if (success) { window.location.href="#/dashboard"; } else { Swal.fire({ type: 'error', title: 'No se pudo auntenticar', text: "Email o contraseña incorrecta" }) } },(err) => { Swal.fire({ type: 'error', title: 'Error en el servidor', text: "No su pudo obtener la informacion" }); }); */