import { Component , OnInit } from '@angular/core' ; import { Router , ActivatedRoute } from '@angular/router' ; import { Title } from '@angular/platform-browser' ; import { UserService } from '../shared/user.service' ; @ Component ( { selector : 'app-login' , templateUrl : './login.component.html' , styleUrls : [ './login.component.scss' ] } ) export class LoginComponent implements OnInit { login = true ; loginErr = 'Invalid email and or password.' ; returnUrl : string ; constructor ( private userService , private router , private route , private titleService ) { } ngOnInit ( ) { this . titleService . setTitle ( 'Login' ) ; this . checkLogin ( ) ; } checkLogin ( ) { const user = this . userService . getUser ( ) ; if ( user ) { this . returnUrl = this . route . snapshot . queryParams . returnUrl || '/' ; this . router . navigateByUrl ( this . returnUrl ) ; } } localAuth ( email , password ) { if ( email && password ) { this . userService . login ( email , password ) . subscribe ( response => { if ( response . success ) { this . login = true ; this . returnUrl = this . route . snapshot . queryParams . returnUrl || '/' ; this . router . navigateByUrl ( this . returnUrl ) ; } else { this . loginErr = response . err ; this . login = false ; } } ) ; } else this . login = false ; } }