import { Component, OnInit } from '@angular/core'; import { FormGroup, FormBuilder } from '@angular/forms'; import { Router } from '@angular/router'; import { AuthService } from '@app/services/auth2.service'; @Component({ selector: 'app-login', templateUrl: './login.component.html', styleUrls: ['./login.component.scss'] }) export class LoginComponent implements OnInit { loginForm: FormGroup; constructor(private authService: AuthService, private formBuilder: FormBuilder, private router: Router) { } ngOnInit() { this.loginForm = this.formBuilder.group({ email: [''], password: [''] }); } get f() { return this.loginForm.controls; } login() { console.log("enter login"); this.authService.login( { email: this.f.email.value, password: this.f.password.value } ) .subscribe(success => { if (success) { console.log(success); console.log("success"); //window.location.href=""; //this.router.navigate(['/secret-random-number']); } }); } } /* import { Component, OnInit } from '@angular/core'; import { Location } from '@angular/common'; import { Router, ActivatedRoute, NavigationEnd } from '@angular/router'; import { NgForm } from '@angular/forms'; import { User } from '../../models/user'; import { AuthService } from '../../services/auth.service'; import Swal from 'sweetalert2'; @Component({ selector: 'app-login', templateUrl: './login.component.html', styleUrls: ['./login.component.scss'] }) export class LoginComponent implements OnInit { user: User = new User(); recordarme = false; returnUrl: string; error_message: string; constructor( private auth: AuthService, private router: Router, private route: ActivatedRoute, private location: Location ) { this.router.routeReuseStrategy.shouldReuseRoute = function(){ return false; } this.router.events.subscribe((evt) => { if (evt instanceof NavigationEnd) { // trick the Router into believing it's last link wasn't previously loaded this.router.navigated = false; // if you need to scroll back to top, here is the right place window.scrollTo(0, 0); } }); } ngOnInit() { this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/'; if ( localStorage.getItem('email') ) { this.user.email = localStorage.getItem('email'); //this.recordarme = true; } } login( form: NgForm ) { if ( form.invalid ) { return; } Swal.fire({ allowOutsideClick: false, type: 'info', text: 'Espere por favor...' }); Swal.showLoading(); this.auth.login( this.user ) .subscribe( resp => { Swal.close(); localStorage.setItem('email', this.user.email); //this.router.navigate([this.returnUrl]); window.location.href=""; }, (err) => { if(err.error.error.message == "INVALID_EMAIL"){ this.error_message = "Correo electrónico inválido"; } else if(err.error.error.message == "INVALID_PASSWORD"){ this.error_message = "Contraseña incorrecta"; } else { this.error_message = "Ha ocurrido un error"; } Swal.fire({ type: 'error', title: 'Error al autenticar', text: this.error_message }); }); } } */