login.component.ts 1.23 KB
Newer Older
cazenave's avatar
cazenave committed
1
import { Component, OnInit } from '@angular/core';
2
3
4
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { AuthService } from './services/auth.service';
cazenave's avatar
cazenave committed
5
6
7
8
9
10
11

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit {
12
13
14
  loginForm: FormGroup;
  isSubmitted  =  false;
  public isLoggedIn = false;
cazenave's avatar
cazenave committed
15

cazenave's avatar
cazenave committed
16

cazenave's avatar
cazenave committed
17

18
19
20
21
22
23
24
25
26
27
  constructor(private authService: AuthService, private router: Router, private formBuilder: FormBuilder) { }

  ngOnInit(): void {
    //Create form
    this.loginForm  =  this.formBuilder.group({
      email: ['', Validators.required],
      password: ['', Validators.required]
    });
    //Check crendentials
    this.isLoggedIn = this.authService.checkCredentials();    
cazenave's avatar
cazenave committed
28
29
30

  }

31
32
33
34
35
36
37
38
39
40
41
  get formControls() { return this.loginForm.controls; }

  login() {
    const val = this.loginForm.value;

    if (val.email && val.password) {
        this.authService.login(val.email, val.password)
            .subscribe(
                () => {
                    console.log("User is logged in");
                    this.router.navigateByUrl('/');
cazenave's avatar
cazenave committed
42
                }
43
44
45
46
            );
    }
}
  
cazenave's avatar
cazenave committed
47

48
  
cazenave's avatar
cazenave committed
49
50

}