source: bus-n-go-pavel-216049/bus-n-go-frontend/src/app/components/register/register.component.ts

Last change on this file was baf4cc4, checked in by ppaunovski <paunovskipavel@…>, 4 weeks ago

split group project and individual project into two separate folders

  • Property mode set to 100644
File size: 2.3 KB
Line 
1import { Component } from '@angular/core';
2import {MatFormField} from "@angular/material/form-field";
3import {MatInput} from "@angular/material/input";
4import {MatButtonModule} from "@angular/material/button";
5import {
6 AbstractControl,
7 Form,
8 FormControl,
9 FormGroup,
10 ReactiveFormsModule,
11 ValidationErrors,
12 ValidatorFn,
13 Validators
14} from "@angular/forms";
15import {AuthService} from "../../services/auth/auth.service";
16import {RegisterRequest} from "../../model/requests/RegisterRequest";
17import {Router} from "@angular/router";
18
19@Component({
20 selector: 'app-register',
21 standalone: true,
22 imports: [
23 MatFormField,
24 MatInput,
25 ReactiveFormsModule,
26 MatButtonModule,
27 ],
28 templateUrl: './register.component.html',
29 styleUrl: './register.component.css'
30})
31export class RegisterComponent {
32
33 form: FormGroup = new FormGroup({
34 email: new FormControl('', [Validators.required, Validators.email]),
35 password: new FormControl('', [Validators.required]),
36 phoneNumber: new FormControl('', [Validators.required]),
37 name : new FormControl('', [Validators.required]),
38 address: new FormControl('', [Validators.required]),
39 confirmPassword: new FormControl('', [Validators.required])
40 },
41 {
42 validators: this.matchValidator('password', 'confirmPassword')
43 })
44
45 constructor(private authService: AuthService, private router: Router) {
46 }
47
48 matchValidator(controlName: string, matchingControlName: string): ValidatorFn {
49 return (abstractControl: AbstractControl) => {
50 const control = abstractControl.get(controlName);
51 const matchingControl = abstractControl.get(matchingControlName);
52
53 if (matchingControl!.errors && !matchingControl!.errors?.['confirmedValidator']) {
54 return null;
55 }
56
57 if (control!.value !== matchingControl!.value) {
58 const error = { confirmedValidator: 'Passwords do not match.' };
59 matchingControl!.setErrors(error);
60 return error;
61 } else {
62 matchingControl!.setErrors(null);
63 return null;
64 }
65 }
66
67
68 }
69
70 onSubmit() {
71 this.authService.register(this.form.value as RegisterRequest).subscribe({
72 next: response => {
73 console.log(response)
74 sessionStorage.setItem('jwt', response.token);
75 this.authService.refreshAuth$.next(true);
76 this.router.navigate(['/'])
77 }
78 }
79 )
80 }
81}
82
Note: See TracBrowser for help on using the repository browser.