login.component.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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(private authService: AuthService, private formBuilder: FormBuilder, private router: Router) { }
  15. ngOnInit() {
  16. this.loginForm = this.formBuilder.group({
  17. email: ['', [Validators.required, Validators.email]],
  18. password: ['', Validators.required]
  19. });
  20. }
  21. get f() { return this.loginForm.controls; }
  22. login() {
  23. this.submitted = true;
  24. // stop here if form is invalid
  25. if (this.loginForm.invalid) {
  26. return;
  27. }
  28. Swal.fire({
  29. allowOutsideClick: false,
  30. type: 'info',
  31. text: 'Espere por favor...'
  32. });
  33. Swal.showLoading();
  34. this.authService.login(
  35. {
  36. email: this.f.email.value,
  37. password: this.f.password.value
  38. }
  39. )
  40. .subscribe(success => {
  41. if (success) {
  42. window.location.href="#/dashboard";
  43. }
  44. else {
  45. Swal.fire({
  46. type: 'error',
  47. title: 'No se pudo auntenticar',
  48. text: "Email o contraseña incorrecta"
  49. })
  50. }
  51. },(err) => {
  52. Swal.fire({
  53. type: 'error',
  54. title: 'Error en el servidor',
  55. text: "No su pudo obtener la informacion"
  56. });
  57. });
  58. }
  59. }
  60. /* import { Component, OnInit } from '@angular/core';
  61. import { Location } from '@angular/common';
  62. import { Router, ActivatedRoute, NavigationEnd } from '@angular/router';
  63. import { NgForm } from '@angular/forms';
  64. import { User } from '../../models/user';
  65. import { AuthService } from '../../services/auth.service';
  66. import Swal from 'sweetalert2';
  67. @Component({
  68. selector: 'app-login',
  69. templateUrl: './login.component.html',
  70. styleUrls: ['./login.component.scss']
  71. })
  72. export class LoginComponent implements OnInit {
  73. user: User = new User();
  74. recordarme = false;
  75. returnUrl: string;
  76. error_message: string;
  77. constructor( private auth: AuthService,
  78. private router: Router,
  79. private route: ActivatedRoute,
  80. private location: Location ) {
  81. this.router.routeReuseStrategy.shouldReuseRoute = function(){
  82. return false;
  83. }
  84. this.router.events.subscribe((evt) => {
  85. if (evt instanceof NavigationEnd) {
  86. // trick the Router into believing it's last link wasn't previously loaded
  87. this.router.navigated = false;
  88. // if you need to scroll back to top, here is the right place
  89. window.scrollTo(0, 0);
  90. }
  91. });
  92. }
  93. ngOnInit() {
  94. this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
  95. if ( localStorage.getItem('email') ) {
  96. this.user.email = localStorage.getItem('email');
  97. //this.recordarme = true;
  98. }
  99. }
  100. login( form: NgForm ) {
  101. if ( form.invalid ) { return; }
  102. Swal.fire({
  103. allowOutsideClick: false,
  104. type: 'info',
  105. text: 'Espere por favor...'
  106. });
  107. Swal.showLoading();
  108. this.auth.login( this.user )
  109. .subscribe( resp => {
  110. Swal.close();
  111. localStorage.setItem('email', this.user.email);
  112. //this.router.navigate([this.returnUrl]);
  113. window.location.href="";
  114. }, (err) => {
  115. if(err.error.error.message == "INVALID_EMAIL"){
  116. this.error_message = "Correo electrónico inválido";
  117. }
  118. else if(err.error.error.message == "INVALID_PASSWORD"){
  119. this.error_message = "Contraseña incorrecta";
  120. }
  121. else {
  122. this.error_message = "Ha ocurrido un error";
  123. }
  124. Swal.fire({
  125. type: 'error',
  126. title: 'Error al autenticar',
  127. text: this.error_message
  128. });
  129. });
  130. }
  131. }
  132. */