Guest User

Untitled

a guest
May 23rd, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. import { Component } from '@angular/core';
  2. import { FormBuilder, FormGroup, Validators } from '@angular/forms';
  3.  
  4. import { Md5 } from 'ts-md5/dist/md5';
  5.  
  6. import { UserService } from './shared/user.service';
  7. import { IUser, IUserAuth, IUserFeatures } from './shared/user.moldes';
  8.  
  9. @Component({
  10. selector: 'app-login',
  11. styleUrls: ['./login.component.scss'],
  12. templateUrl: './login.component.html'
  13. })
  14. /**
  15. * Class representing the LoginComponent.
  16. */
  17. export class LoginComponent {
  18. public userForm: FormGroup;
  19.  
  20. public username: string;
  21. public password: string;
  22. public submitted: boolean;
  23.  
  24. constructor(private formBuilder: FormBuilder) {
  25. this.submitted = false;
  26. this.buildForm();
  27. }
  28.  
  29. /**
  30. * Triggers when the user click on the login button.
  31. */
  32. public onSubmit(): void {
  33. this.submitted = true;
  34. if (!this.userForm.valid) {
  35. return;
  36. }
  37. const user: IUser = {
  38. email: this.userForm.controls.username.value,
  39. password: Md5.hashStr(this.userForm.controls.password.value),
  40. };
  41. this.userService.authenticateUser(user).subscribe(() => {
  42. //
  43. });
  44. }
  45.  
  46. /**
  47. * Builds the user login form.
  48. */
  49. private buildForm(): void {
  50. this.userForm = this.formBuilder.group({
  51. username: [null,[Validators.required],
  52. password: [null, Validators.required]
  53. });
  54. }
  55. }
  56.  
  57. <form [formGroup]="userForm" (ngSubmit)="onSubmit()" novalidate class="form-wrapper">
  58.  
  59. <div class="form-group block"
  60. [ngClass]="{'has-error': submitted && (userForm.controls.username.errors)}">
  61. <label>Username</label>
  62. <input type="text" class="form-control" formControlName="username">
  63. <p>
  64. <span *ngIf="submitted && userForm.controls.username.errors?.required">Username is required</span>
  65. </p>
  66. </div>
  67.  
  68. <div class="form-group block"
  69. [ngClass]="{'has-error': submitted && (userForm.controls.password.errors)}">
  70. <label>Password</label>
  71. <input type="password" class="form-control" formControlName="password">
  72. <p *ngIf="submitted && userForm.controls.password.errors?.required">Password is required</p>
  73. </div>
  74.  
  75. <button type="submit" class="btn btn-default">Login</button>
  76. <div>
  77. <div *ngIF="submitted && userForm.valid">Tick</div>
  78. <div *ngIF="submitted && !userForm.valid">X</div>
  79. </div>
  80. </form>
Add Comment
Please, Sign In to add comment