login.component.ts 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { Component, OnInit } from "@angular/core";
  2. import { FormGroup, FormBuilder, Validators } from "@angular/forms";
  3. import { Router } from "@angular/router";
  4. import { AuthService } from "@app/services/auth2.service";
  5. import Swal from "sweetalert2";
  6. @Component({
  7. selector: "app-login",
  8. templateUrl: "./login.component.html",
  9. styleUrls: ["./login.component.scss"]
  10. })
  11. export class LoginComponent implements OnInit {
  12. loginForm: FormGroup;
  13. submitted: boolean = false;
  14. constructor(
  15. private authService: AuthService,
  16. private formBuilder: FormBuilder,
  17. private router: Router
  18. ) {}
  19. ngOnInit() {
  20. this.loginForm = this.formBuilder.group({
  21. username: ["", [Validators.required]],
  22. password: ["", Validators.required]
  23. });
  24. }
  25. get f() {
  26. return this.loginForm.controls;
  27. }
  28. login() {
  29. this.submitted = true;
  30. // stop here if form is invalid
  31. if (this.loginForm.invalid) {
  32. return;
  33. }
  34. Swal.fire({
  35. allowOutsideClick: false,
  36. icon: "info",
  37. text: "Espere por favor..."
  38. });
  39. Swal.showLoading();
  40. this.authService
  41. .login({
  42. username: this.f.username.value,
  43. password: this.f.password.value
  44. })
  45. .subscribe(
  46. success => {
  47. if (success) {
  48. window.location.href = "#/investment-proposals";
  49. } else {
  50. Swal.fire({
  51. icon: "warning",
  52. title: "No se pudo auntenticar",
  53. text: "Usuario o contraseña inválidos"
  54. });
  55. }
  56. },
  57. err => {
  58. console.log(err);
  59. Swal.fire({
  60. icon: "error",
  61. title: "Error al autenticar",
  62. text: "Usuario o contraseña inválidos"
  63. });
  64. }
  65. );
  66. }
  67. }