import { Component , OnInit } from '@angular/core' ; import { FormBuilder , FormGroup , FormControl , Validators } from '@angular/forms' ; import { ActivatedRoute , Router } from '@angular/router' ; import { Errors , UserService } from '../core' ; @ Component ( { selector : 'app-auth-page' , templateUrl : './auth.component.html' } ) export class AuthComponent implements OnInit { authType : String = '' ; title : String = '' ; errors : Errors = { errors : { } } ; isSubmitting = false ; authForm : FormGroup ; constructor ( private route , private router , private userService , private fb ) { this . authForm = this . fb . group ( { 'email' : [ '' , Validators . required ] , 'password' : [ '' , Validators . required ] } ) ; } ngOnInit ( ) { this . route . url . subscribe ( data => { this . authType = data [ data . length - 1 ] . path ; this . title = ( this . authType === 'login' ) ? 'Sign in' : 'Sign up' ; if ( this . authType === 'register' ) { this . authForm . addControl ( 'username' , new FormControl ( ) ) ; } } ) ; } submitForm ( ) { this . isSubmitting = true ; this . errors = { errors : { } } ; const credentials = this . authForm . value ; this . userService . attemptAuth ( this . authType , credentials ) . subscribe ( data => this . router . navigateByUrl ( '/' ) , err => { this . errors = err ; this . isSubmitting = false ; } ) ; } }