Changeset b738035 for trip-planner-front/src/app/homepage/login
- Timestamp:
- 01/24/22 21:08:32 (3 years ago)
- Branches:
- master
- Children:
- 76712b2
- Parents:
- bdd6491
- Location:
- trip-planner-front/src/app/homepage/login
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trip-planner-front/src/app/homepage/login/login.component.html
rbdd6491 rb738035 1 <div class="p-col-12 p-md-4"> 2 <div class="p-inputgroup"> 3 <span class="p-inputgroup-addon"><i class="pi pi-user"></i></span> 4 <input type="text" pInputText placeholder="Email address (Username)" 5 [(ngModel)]="loginRequest.username" name="username"> 1 <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" 2 integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous"> 3 4 5 6 <div class="readersack"> 7 <div class="container"> 8 <div class="row"> 9 <div class="col-md-6 offset-md-3"> 10 <form [formGroup]="form" #myform="ngForm" (ngSubmit)="onFormLogIn(loginRequest)"> 11 <div class="form-group"> 12 13 <label>Username (email)</label> 14 <input formControlName="username" id="username" type="text" class="form-control" 15 [(ngModel)]="loginRequest.username" 16 [ngClass]="{ 'is-invalid': myform.submitted && (form.get('username')?.errors?.['required'] 17 || form.get('username')?.errors?.['validateUser'])}" /> 18 <div *ngIf="form.get('username')?.hasError('required') && myform.submitted" 19 class="invalid-feedback"> 20 Username is required 21 </div> 22 <div *ngIf="form.get('username')?.hasError('validateUser') && myform.submitted" 23 class="invalid-feedback"> 24 Invalid username 25 </div> 26 27 <label>Password</label> 28 <input formControlName="password" id="password" type="password" class="form-control" 29 [(ngModel)]="loginRequest.password" [ngClass]="{ 'is-invalid': myform.submitted && form.get('password')?.errors?.['required']}" /> 30 <div *ngIf="form.get('password')?.hasError('required') && myform.submitted" 31 class="invalid-feedback"> 32 Password is required</div> 33 34 35 <div class="p-col-12 p-md-4"> 36 <button type="submit" pButton pRipple class="p-button-secondary">Submit</button> 37 </div> 38 </div> 39 </form> 40 </div> 41 </div> 6 42 </div> 7 43 </div> 8 <div class="p-col-12 p-md-4">9 <div class="p-inputgroup">10 <span class="p-inputgroup-addon"><i class="pi pi-password"></i></span>11 <input type="password" pInputText placeholder="Password" name="password" [(ngModel)]="loginRequest.password">12 </div>13 </div>14 15 <div class="p-col-12 p-md-4">16 <button pButton pRipple label="Log in" class="p-button-secondary" type="submit"17 (click)="onFormLogIn(loginRequest)"></button>18 </div> -
trip-planner-front/src/app/homepage/login/login.component.ts
rbdd6491 rb738035 1 1 import { Component, OnInit } from '@angular/core'; 2 import { Router } from '@angular/router';2 import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms'; 3 3 import { DynamicDialogRef } from 'primeng/dynamicdialog'; 4 4 import { LoginRequest } from 'src/app/_models/dto/loginRequest'; 5 import { PlannerLocationDto } from 'src/app/_models/dto/plannerLocationDto'; 6 import { UserDto } from 'src/app/_models/dto/userDto'; 7 import { UsernameDto } from 'src/app/_models/dto/usernameDto'; 5 8 import { UserService } from 'src/app/_services/user.service'; 6 9 … … 12 15 export class LoginComponent implements OnInit { 13 16 17 form: FormGroup; 18 loginRequest: LoginRequest; 19 usernames: string[]; 14 20 15 loginRequest : LoginRequest; 21 constructor(private ref: DynamicDialogRef, private userService: UserService, formBuilder: FormBuilder) { 22 this.loginRequest = new LoginRequest(); 23 this.usernames = []; 16 24 17 constructor(private ref: DynamicDialogRef, private router: Router, private userService : UserService) { 18 this.loginRequest = new LoginRequest(); 19 25 this.form = formBuilder.group({ 26 username: new FormControl('', [Validators.required]), 27 password: new FormControl('', [Validators.required, Validators.minLength(6)]), 28 }, 29 { 30 validators: [ 31 this.validateUser('username') 32 ] 33 } 34 ); 20 35 } 21 36 22 37 ngOnInit(): void { 38 23 39 } 24 40 25 onFormLogIn(loginRequest){ 26 this.ref.close(loginRequest); 27 41 onFormLogIn(loginRequest) { 42 const { valid } = this.form; 43 if (valid) { 44 this.ref.close(loginRequest); 45 } 46 } 47 48 validateUser(username: string) { 49 return (formGroup: FormGroup) => { 50 const control = formGroup.controls[username]; 51 52 if (control.errors && !control.errors.validateUser) { 53 return; 54 } 55 56 this.userService.getAllUsernames().subscribe( 57 data => { 58 this.usernames = data; 59 for (let i = 0; i < this.usernames.length; i++) { 60 if (control.value === this.usernames[i]) { 61 control.setErrors(null); 62 63 break; 64 } else { 65 control.setErrors({ validateUser: true }); 66 } 67 68 } 69 } 70 ); 71 return null; 72 73 }; 74 28 75 } 29 76 }
Note:
See TracChangeset
for help on using the changeset viewer.