|
|
@@ -0,0 +1,49 @@
|
|
|
+import { Component, OnInit } from '@angular/core';
|
|
|
+import { CommonModule } from '@angular/common';
|
|
|
+import { ReactiveFormsModule, FormBuilder, FormGroup, Validators } from '@angular/forms';
|
|
|
+import { Router } from '@angular/router';
|
|
|
+import { AuthService } from '../services/auth.service';
|
|
|
+
|
|
|
+@Component({
|
|
|
+ standalone: true,
|
|
|
+ selector: 'app-register',
|
|
|
+ imports: [CommonModule, ReactiveFormsModule],
|
|
|
+ templateUrl: './register.component.html'
|
|
|
+})
|
|
|
+export class RegisterComponent implements OnInit {
|
|
|
+ form!: FormGroup;
|
|
|
+
|
|
|
+ constructor(
|
|
|
+ private fb: FormBuilder,
|
|
|
+ private router: Router,
|
|
|
+ private auth: AuthService
|
|
|
+ ) {}
|
|
|
+
|
|
|
+ ngOnInit(): void {
|
|
|
+ this.form = this.fb.group({
|
|
|
+ name: ['', Validators.required],
|
|
|
+ email: ['', [Validators.required, Validators.email]],
|
|
|
+ password: ['', [Validators.required, Validators.minLength(6)]]
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ onSubmit(): void {
|
|
|
+ if (this.form.invalid) {
|
|
|
+ this.form.markAllAsTouched();
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const formValue = this.form.value;
|
|
|
+
|
|
|
+ this.auth.register(formValue).subscribe({
|
|
|
+ next: (res) => {
|
|
|
+ this.auth.storeToken(res.token);
|
|
|
+ this.router.navigate(['/dashboard']);
|
|
|
+ },
|
|
|
+ error: (err) => {
|
|
|
+ console.error('Registration failed', err);
|
|
|
+ alert('Registration failed: ' + (err.error?.message || 'Unknown error'));
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|