login.component.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { Component, OnInit } from '@angular/core';
  2. import { FormGroup, FormBuilder } from '@angular/forms';
  3. import { Router } from '@angular/router';
  4. import { AuthService } from '@app/services/auth2.service';
  5. @Component({
  6. selector: 'app-login',
  7. templateUrl: './login.component.html',
  8. styleUrls: ['./login.component.scss']
  9. })
  10. export class LoginComponent implements OnInit {
  11. loginForm: FormGroup;
  12. constructor(private authService: AuthService, private formBuilder: FormBuilder, private router: Router) { }
  13. ngOnInit() {
  14. this.loginForm = this.formBuilder.group({
  15. email: [''],
  16. password: ['']
  17. });
  18. }
  19. get f() { return this.loginForm.controls; }
  20. login() {
  21. console.log("enter login");
  22. this.authService.login(
  23. {
  24. email: this.f.email.value,
  25. password: this.f.password.value
  26. }
  27. )
  28. .subscribe(success => {
  29. if (success) {
  30. console.log(success);
  31. console.log("success");
  32. //window.location.href="";
  33. //this.router.navigate(['/secret-random-number']);
  34. }
  35. });
  36. }
  37. }
  38. /* import { Component, OnInit } from '@angular/core';
  39. import { Location } from '@angular/common';
  40. import { Router, ActivatedRoute, NavigationEnd } from '@angular/router';
  41. import { NgForm } from '@angular/forms';
  42. import { User } from '../../models/user';
  43. import { AuthService } from '../../services/auth.service';
  44. import Swal from 'sweetalert2';
  45. @Component({
  46. selector: 'app-login',
  47. templateUrl: './login.component.html',
  48. styleUrls: ['./login.component.scss']
  49. })
  50. export class LoginComponent implements OnInit {
  51. user: User = new User();
  52. recordarme = false;
  53. returnUrl: string;
  54. error_message: string;
  55. constructor( private auth: AuthService,
  56. private router: Router,
  57. private route: ActivatedRoute,
  58. private location: Location ) {
  59. this.router.routeReuseStrategy.shouldReuseRoute = function(){
  60. return false;
  61. }
  62. this.router.events.subscribe((evt) => {
  63. if (evt instanceof NavigationEnd) {
  64. // trick the Router into believing it's last link wasn't previously loaded
  65. this.router.navigated = false;
  66. // if you need to scroll back to top, here is the right place
  67. window.scrollTo(0, 0);
  68. }
  69. });
  70. }
  71. ngOnInit() {
  72. this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
  73. if ( localStorage.getItem('email') ) {
  74. this.user.email = localStorage.getItem('email');
  75. //this.recordarme = true;
  76. }
  77. }
  78. login( form: NgForm ) {
  79. if ( form.invalid ) { return; }
  80. Swal.fire({
  81. allowOutsideClick: false,
  82. type: 'info',
  83. text: 'Espere por favor...'
  84. });
  85. Swal.showLoading();
  86. this.auth.login( this.user )
  87. .subscribe( resp => {
  88. Swal.close();
  89. localStorage.setItem('email', this.user.email);
  90. //this.router.navigate([this.returnUrl]);
  91. window.location.href="";
  92. }, (err) => {
  93. if(err.error.error.message == "INVALID_EMAIL"){
  94. this.error_message = "Correo electrónico inválido";
  95. }
  96. else if(err.error.error.message == "INVALID_PASSWORD"){
  97. this.error_message = "Contraseña incorrecta";
  98. }
  99. else {
  100. this.error_message = "Ha ocurrido un error";
  101. }
  102. Swal.fire({
  103. type: 'error',
  104. title: 'Error al autenticar',
  105. text: this.error_message
  106. });
  107. });
  108. }
  109. }
  110. */