[baf4cc4] | 1 | import { Component } from '@angular/core';
|
---|
| 2 | import {MatFormField} from "@angular/material/form-field";
|
---|
| 3 | import {MatInput} from "@angular/material/input";
|
---|
| 4 | import {MatButtonModule} from "@angular/material/button";
|
---|
| 5 | import {
|
---|
| 6 | AbstractControl,
|
---|
| 7 | Form,
|
---|
| 8 | FormControl,
|
---|
| 9 | FormGroup,
|
---|
| 10 | ReactiveFormsModule,
|
---|
| 11 | ValidationErrors,
|
---|
| 12 | ValidatorFn,
|
---|
| 13 | Validators
|
---|
| 14 | } from "@angular/forms";
|
---|
| 15 | import {AuthService} from "../../services/auth/auth.service";
|
---|
| 16 | import {RegisterRequest} from "../../model/requests/RegisterRequest";
|
---|
| 17 | import {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 | })
|
---|
| 31 | export 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 |
|
---|