new-password.component.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import { Component, OnInit, ViewChild } from "@angular/core";
  2. import { ActivatedRoute } from "@angular/router";
  3. import { FormGroup, FormBuilder, Validators } from "@angular/forms";
  4. import { UserService } from "@app/services/user.service";
  5. import Swal from "sweetalert2";
  6. import {
  7. ValidatorComponent,
  8. PasswordStrengthValidator,
  9. } from "../plugins/validator/validator.component";
  10. @Component({
  11. selector: "app-new-password",
  12. templateUrl: "./new-password.component.html",
  13. styleUrls: ["./new-password.component.scss"],
  14. })
  15. export class NewPasswordComponent implements OnInit {
  16. token: string;
  17. validToken: boolean;
  18. activateForm: FormGroup;
  19. errorMessage: string;
  20. userActivated: boolean;
  21. successActivation: boolean;
  22. activateMessage: string;
  23. invalidToken: boolean;
  24. submitted: boolean = false;
  25. showDetails: boolean;
  26. newPass: boolean;
  27. constructor(
  28. private formBuilder: FormBuilder,
  29. private route: ActivatedRoute,
  30. private userService: UserService
  31. ) {
  32. this.route.queryParams.subscribe((params) => {
  33. this.token = params["token"];
  34. });
  35. }
  36. ngOnInit() {
  37. if (this.token !== null) {
  38. this.userService
  39. .validateUserToken({
  40. token: this.token,
  41. process: "pwd_recovery",
  42. })
  43. .subscribe(
  44. (res) => {
  45. console.log(res);
  46. let userData = res["data"].user;
  47. this.newPass = true;
  48. this.validToken = true;
  49. this.activateForm = this.formBuilder.group(
  50. {
  51. // Load information
  52. password: [
  53. "",
  54. [
  55. Validators.required,
  56. Validators.minLength(8),
  57. PasswordStrengthValidator,
  58. ],
  59. ],
  60. confirm_password: ["", Validators.required],
  61. },
  62. {
  63. validator: ValidatorComponent("password", "confirm_password"),
  64. }
  65. );
  66. },
  67. (err) => {
  68. this.newPass = false;
  69. this.userActivated = true;
  70. this.invalidToken = true;
  71. this.errorMessage = err.message;
  72. }
  73. );
  74. } else {
  75. this.invalidToken = true;
  76. this.errorMessage = "No existe el token";
  77. }
  78. }
  79. get f() {
  80. return this.activateForm.controls;
  81. }
  82. newPassword() {
  83. this.submitted = true;
  84. // stop here if form is invalid
  85. if (this.activateForm.invalid) {
  86. return;
  87. }
  88. this.userService
  89. .changePassword({
  90. password: this.f.password.value,
  91. confirm_password: this.f.confirm_password.value,
  92. /*phone_number: this.f.phone_number.value,
  93. distribuidora: this.f.distribuidora.value,
  94. cod_tarifa: this.f.cod_tarifa.value*/
  95. })
  96. .subscribe(
  97. (res) => {
  98. this.successActivation = true;
  99. this.validToken = false;
  100. this.activateMessage = "Su clave ha sido actualizada exitosamente";
  101. },
  102. (err) => {
  103. this.validToken = false;
  104. this.errorMessage = err.message;
  105. }
  106. );
  107. //
  108. }
  109. }
  110. /*
  111. Swal.fire({
  112. allowOutsideClick: false,
  113. type: 'info',
  114. text: 'Espere por favor...'
  115. });
  116. Swal.showLoading();
  117. this.authService.login(
  118. {
  119. email: this.f.email.value,
  120. password: this.f.password.value
  121. }
  122. )
  123. .subscribe(success => {
  124. if (success) {
  125. window.location.href="#/dashboard";
  126. }
  127. else {
  128. Swal.fire({
  129. type: 'error',
  130. title: 'No se pudo auntenticar',
  131. text: "Email o contraseña incorrecta"
  132. })
  133. }
  134. },(err) => {
  135. Swal.fire({
  136. type: 'error',
  137. title: 'Error en el servidor',
  138. text: "No su pudo obtener la informacion"
  139. });
  140. });
  141. */