new-password.component.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. userData: any;
  28. sendOtherToken: boolean;
  29. constructor(
  30. private formBuilder: FormBuilder,
  31. private route: ActivatedRoute,
  32. private userService: UserService
  33. ) {
  34. this.route.queryParams.subscribe((params) => {
  35. this.token = params["token"];
  36. });
  37. }
  38. ngOnInit() {
  39. Swal.fire({
  40. allowOutsideClick: false,
  41. type: "info",
  42. text: "Espere por favor...",
  43. });
  44. Swal.showLoading();
  45. if (this.token !== null) {
  46. this.userService
  47. .validateUserToken({
  48. token: this.token,
  49. process: "pwd_recovery",
  50. })
  51. .subscribe(
  52. (res) => {
  53. Swal.close();
  54. this.sendOtherToken = false;
  55. this.userData = res["data"].user;
  56. this.newPass = true;
  57. this.validToken = true;
  58. this.activateForm = this.formBuilder.group(
  59. {
  60. // Load information
  61. password: [
  62. "",
  63. [
  64. Validators.required,
  65. Validators.minLength(8),
  66. PasswordStrengthValidator,
  67. ],
  68. ],
  69. confirm_password: ["", Validators.required],
  70. },
  71. {
  72. validator: ValidatorComponent("password", "confirm_password"),
  73. }
  74. );
  75. },
  76. (err) => {
  77. Swal.close();
  78. this.newPass = false;
  79. this.userActivated = true;
  80. this.invalidToken = true;
  81. this.errorMessage = err.message;
  82. this.sendOtherToken = true;
  83. }
  84. );
  85. } else {
  86. this.invalidToken = true;
  87. this.errorMessage = "No existe el token";
  88. }
  89. }
  90. getNewToken() {
  91. Swal.fire({
  92. allowOutsideClick: false,
  93. type: "info",
  94. text: "Espere por favor...",
  95. });
  96. Swal.showLoading();
  97. this.userService
  98. .sendAnotherToken({
  99. token: this.token,
  100. })
  101. .subscribe(
  102. (res) => {
  103. Swal.fire({
  104. type: "success",
  105. title: "Exito",
  106. text: "Se ha enviado otro correo para recuperación de contraseña",
  107. }).then((result) => {
  108. this.successActivation = true;
  109. this.validToken = false;
  110. this.invalidToken = false;
  111. window.location.href = "#/investment-proposals";
  112. });
  113. },
  114. (err) => {
  115. Swal.close();
  116. this.validToken = false;
  117. this.errorMessage = err.message;
  118. }
  119. );
  120. }
  121. get f() {
  122. return this.activateForm.controls;
  123. }
  124. newPassword() {
  125. this.submitted = true;
  126. // stop here if form is invalid
  127. if (this.activateForm.invalid) {
  128. return;
  129. }
  130. Swal.fire({
  131. allowOutsideClick: false,
  132. type: "info",
  133. text: "Espere por favor...",
  134. });
  135. Swal.showLoading();
  136. this.userService
  137. .changePassword({
  138. password: this.f.password.value,
  139. confirm_password: this.f.confirm_password.value,
  140. email: this.userData.email,
  141. first_name: this.userData.first_name,
  142. last_name: this.userData.last_name,
  143. phone_number: this.userData.phone_number,
  144. })
  145. .subscribe(
  146. (res) => {
  147. Swal.fire({
  148. type: "success",
  149. title: "Exito",
  150. text: "Su clave ha sido actualizada exitosamente",
  151. }).then((result) => {
  152. this.successActivation = true;
  153. this.validToken = false;
  154. window.location.href = "#/investment-proposals";
  155. });
  156. },
  157. (err) => {
  158. Swal.fire({
  159. type: "error",
  160. title: "Error en el servidor",
  161. text: err.message,
  162. });
  163. this.validToken = false;
  164. }
  165. );
  166. //
  167. }
  168. }
  169. /*
  170. Swal.fire({
  171. allowOutsideClick: false,
  172. type: 'info',
  173. text: 'Espere por favor...'
  174. });
  175. Swal.showLoading();
  176. this.authService.login(
  177. {
  178. email: this.f.email.value,
  179. password: this.f.password.value
  180. }
  181. )
  182. .subscribe(success => {
  183. if (success) {
  184. window.location.href="#/dashboard";
  185. }
  186. else {
  187. Swal.fire({
  188. type: 'error',
  189. title: 'No se pudo auntenticar',
  190. text: "Email o contraseña incorrecta"
  191. })
  192. }
  193. },(err) => {
  194. Swal.fire({
  195. type: 'error',
  196. title: 'Error en el servidor',
  197. text: "No su pudo obtener la informacion"
  198. });
  199. });
  200. */