Advertisement
Guest User

Untitled

a guest
Sep 18th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. export class LoginComponent {
  2. credentials: LoginRequest;
  3. error = '';
  4. formGroup: FormGroup;
  5. constructor(private auth: AuthService, private router: Router) {
  6.  
  7. }
  8. ngOnInit() {
  9. this.formGroup = new FormGroup({
  10. username: new FormControl("", Validators.required),
  11. password: new FormControl("", Validators.required)
  12. });
  13. this.credentials = new LoginRequest();
  14. this.auth.logout();
  15. }
  16. login(): Observable<boolean> {
  17. if (this.formGroup.invalid) {
  18. this.markControls(this.formGroup, control => control.markAsDirty());
  19. console.warn("Cannot login, formGroup is invalid!");
  20. return;
  21. }
  22.  
  23. const loggedUser: LoggedUser = {
  24. username: this.formGroup.value.username
  25. };
  26. try
  27. {
  28. this.auth.login(this.formGroup.value)
  29. .subscribe(
  30. result => {
  31. if (result === true) {
  32. this.router.navigate(['/']);
  33. console.log("Login success!");
  34. }
  35. });
  36. }
  37. catch (ex)
  38. {
  39. this.error = 'Nieprawidłowy login lub hasło!';
  40. }
  41. }
  42. private markControls(form: FormGroup, callback: (control: AbstractControl) => void): void {
  43. Object.keys(form.controls)
  44. .forEach(key => {
  45. callback(form.get(key));
  46. })
  47. }
  48.  
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement