60 lines
2.1 KiB
TypeScript
60 lines
2.1 KiB
TypeScript
import { CommonModule } from '@angular/common';
|
|
import { Component, inject } from '@angular/core';
|
|
import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms';
|
|
import { Router, RouterLink } from '@angular/router';
|
|
import { TranslateModule } from '@ngx-translate/core';
|
|
import { ButtonModule } from 'primeng/button';
|
|
import { InputTextModule } from 'primeng/inputtext';
|
|
|
|
import { AuthService } from '../../core/services/auth.service';
|
|
import { UiService } from '../../core/services/ui.service';
|
|
import { AuthToolbarComponent } from '../../shared/auth/auth-toolbar.component';
|
|
|
|
@Component({
|
|
standalone: true,
|
|
imports: [CommonModule, ReactiveFormsModule, RouterLink, TranslateModule, ButtonModule, InputTextModule, AuthToolbarComponent],
|
|
templateUrl: './register-page.component.html'
|
|
})
|
|
export class RegisterPageComponent {
|
|
private readonly fb = inject(FormBuilder);
|
|
private readonly auth = inject(AuthService);
|
|
private readonly router = inject(Router);
|
|
private readonly ui = inject(UiService);
|
|
|
|
error = '';
|
|
success = '';
|
|
submitting = false;
|
|
readonly form = this.fb.nonNullable.group({
|
|
username: ['', [Validators.required, Validators.minLength(3)]],
|
|
password: ['', [Validators.required, Validators.minLength(4)]],
|
|
confirmPassword: ['', [Validators.required, Validators.minLength(4)]]
|
|
});
|
|
|
|
submit() {
|
|
if (this.form.invalid || this.submitting) {
|
|
return;
|
|
}
|
|
this.error = '';
|
|
this.success = '';
|
|
const { username, password, confirmPassword } = this.form.getRawValue();
|
|
if (password !== confirmPassword) {
|
|
this.error = this.ui.instant('auth.passwordsMismatch');
|
|
return;
|
|
}
|
|
this.submitting = true;
|
|
this.auth.register(username, password).subscribe({
|
|
next: () => {
|
|
this.success = this.ui.instant('auth.accountCreated');
|
|
setTimeout(() => this.router.navigate(['/login']), 500);
|
|
},
|
|
error: (err) => {
|
|
this.error = err?.error?.detail ?? this.ui.instant('auth.registrationFailed');
|
|
this.submitting = false;
|
|
},
|
|
complete: () => {
|
|
this.submitting = false;
|
|
}
|
|
});
|
|
}
|
|
}
|