Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Component, OnInit } from '@angular/core';
- import { FormBuilder, FormGroup, Validators } from '@angular/forms';
- import { AuthService } from '../auth/auth.service';
- import { catchError } from 'rxjs/operators';
- import { Observable, throwError } from 'rxjs';
- import { Router } from '@angular/router';
- import { HttpErrorResponse } from '@angular/common/http';
- @Component({
- selector: 'app-login',
- templateUrl: './login.component.html',
- styleUrls: ['./login.component.scss']
- })
- export class LoginComponent implements OnInit {
- loginForm: FormGroup;
- constructor(
- private formBuilder: FormBuilder,
- private authService: AuthService,
- private router: Router
- ) {
- this.loginForm = formBuilder.group ({
- username: ['', Validators.required],
- password: ['', Validators.required]
- });
- }
- ngOnInit() {
- }
- login(){
- this.authService.login(
- this.loginForm.get('username').value,
- this.loginForm.get('password').value
- ).pipe(
- catchError(
- (err: HttpErrorResponse, caught: Observable<Boolean>) => {
- console.log(`ERROR while login: ${err}`);
- return throwError(err);
- }
- )
- ).subscribe(response => {
- if (response) {
- // login successfull...navigate to main page
- this.router.navigateByUrl('/main');
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement