SignInComponent

mail@pastecode.io avatar
unknown
typescript
2 years ago
1.3 kB
6
Indexable
Never
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { FormBuilder, AbstractControl, AsyncValidatorFn } from '@angular/forms';
import { map} from 'rxjs';
import { UsersService } from 'src/app/services/users.service';

@Component({
  selector: 'app-sign-in',
  templateUrl: './sign-in.component.html',
  host: { class: 'login-box'},
  encapsulation: ViewEncapsulation.None,
  styleUrls: ['./sign-in.component.scss']
})
export class SignInComponent implements OnInit {

  signinForm: any;

  constructor(
    private fb: FormBuilder
  ) { }

  ngOnInit(): void {

    this.signinForm = this.fb.group({
      email: ['', {
        validators: [],
        asyncValidators: [this.userExistsValidator(this.email?.value)]
      }],
      password: []
    })
  }


  userExistsValidator(usersService: UsersService): AsyncValidatorFn {
    return (email: AbstractControl) => {
      return usersService.findUserByEmail(email?.value)
        .pipe(
          map(user => user ? { userExists: true } : null)
        );
    }
  }


  get email() {
    return this.signinForm.get('email');
  }
  get password() {
    return this.signinForm.get('password');
  }

  onSubmit() {
    if (this.signinForm.invalid) {
      return;
    }


  }

}