Skip to content
Snippets Groups Projects
Commit b0737484 authored by migudel's avatar migudel :speech_balloon:
Browse files

Revert "Funciona register"

This reverts commit 471c0d62.
parent 2cf09831
No related branches found
No related tags found
2 merge requests!26Revert "Funciona register",!25Dev/angular microservicios
......@@ -6,7 +6,6 @@ import { MainPageComponent } from './core/features/user/main-page/main-page.comp
import { BookingListComponent } from './core/features/bookings/booking-list/booking-list.component';
import { UserBookingListComponent } from './core/features/user/user-booking-list/user-booking-list.component';
import { LoginComponent } from './core/features/auth/login/login.component';
import { RegisterComponent } from './core/features/auth/register/register.component';
export const routes: Routes = [
{
......@@ -41,10 +40,6 @@ export const routes: Routes = [
path: 'auth/login',
component: LoginComponent,
},
{
path: 'auth/register',
component: RegisterComponent,
},
{
path: '**',
redirectTo: '',
......
import { Component } from '@angular/core';
import { FormBuilder, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { HttpClient } from '@angular/common/http';
import { Router } from '@angular/router';
import { CommonModule } from '@angular/common';
@Component({
standalone: true,
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css'],
imports: [CommonModule, ReactiveFormsModule]
imports: [ReactiveFormsModule]
})
export class LoginComponent {
loginForm: FormGroup;
constructor(
private fb: FormBuilder,
private http: HttpClient,
private router: Router
) {
constructor(private fb: FormBuilder) {
this.loginForm = this.fb.group({
email: ['', [Validators.required, Validators.email]],
password: ['', [Validators.required]],
password: ['', Validators.required]
});
}
onSubmit() {
if (this.loginForm.valid) {
const { email, password } = this.loginForm.value;
// Realizar la solicitud al backend
this.http.post('http://localhost:8101', { email, password }).subscribe({
next: (response: any) => {
// Guardar el token en localStorage
localStorage.setItem('authToken', response.token);
// Redirigir al dashboard
this.router.navigate(['/']);
},
error: (err) => {
console.error('Error en el login:', err);
},
});
}
console.log('Login data:', { email, password });
// Aquí iría el llamado al servicio de login con JWT
}
isAuthenticated(): boolean {
return !!localStorage.getItem('authToken');
}
}
<div class="register-container">
<h2>Regístrate</h2>
<h2>Register</h2>
<form [formGroup]="registerForm" (ngSubmit)="onSubmit()">
<div class="form-group">
<label for="name">Nombre</label>
<input
id="name"
type="text"
formControlName="name"
placeholder="Introduce tu nombre"
required
/>
<div *ngIf="registerForm.get('name')?.invalid && registerForm.get('name')?.touched">
<small>El nombre es obligatorio y debe tener al menos 3 caracteres.</small>
</div>
</div>
<div class="form-group">
<label for="email">Correo Electrónico</label>
<input
id="email"
type="email"
formControlName="email"
placeholder="Introduce tu correo"
required
/>
<div>
<label for="email">Email:</label>
<input id="email" type="email" formControlName="email" />
<div *ngIf="registerForm.get('email')?.invalid && registerForm.get('email')?.touched">
<small>El correo electrónico no es válido.</small>
<small>Email is required and must be valid.</small>
</div>
</div>
<div class="form-group">
<label for="password">Contraseña</label>
<input
id="password"
type="password"
formControlName="password"
placeholder="Introduce tu contraseña"
required
/>
<div>
<label for="password">Password:</label>
<input id="password" type="password" formControlName="password" />
<div *ngIf="registerForm.get('password')?.invalid && registerForm.get('password')?.touched">
<small>La contraseña debe tener al menos 6 caracteres.</small>
<small>Password is required.</small>
</div>
</div>
<button type="submit" [disabled]="registerForm.invalid">Registrarse</button>
<div *ngIf="errorMessage" class="error-message">
{{ errorMessage }}
<div>
<label for="confirmPassword">Confirm Password:</label>
<input id="confirmPassword" type="password" formControlName="confirmPassword" />
<div *ngIf="registerForm.get('confirmPassword')?.invalid && registerForm.get('confirmPassword')?.touched">
<small>Passwords must match.</small>
</div>
</div>
<button type="submit" [disabled]="registerForm.invalid">Register</button>
</form>
</div>
\ No newline at end of file
import { CommonModule } from '@angular/common';
import { HttpClient } from '@angular/common/http';
import { Component } from '@angular/core';
import { FormBuilder, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
@Component({
standalone: true,
selector: 'app-register',
templateUrl: './register.component.html',
styleUrls: ['./register.component.css'],
imports: [ReactiveFormsModule, CommonModule]
styleUrls: ['./register.component.css']
})
export class RegisterComponent {
registerForm: FormGroup;
errorMessage: string | null = null;
constructor(
private fb: FormBuilder,
private http: HttpClient,
private router: Router
) {
constructor(private fb: FormBuilder) {
this.registerForm = this.fb.group({
name: ['', [Validators.required, Validators.minLength(3)]],
email: ['', [Validators.required, Validators.email]],
password: ['', [Validators.required, Validators.minLength(6)]],
password: ['', Validators.required],
confirmPassword: ['', Validators.required]
});
}
onSubmit() {
if (this.registerForm.valid) {
const { name, email, password } = this.registerForm.value;
// Enviar solicitud al backend
this.http.post('http://localhost:8080/users', { name, email, password }).subscribe({
next: () => {
alert('Usuario registrado con éxito.');
this.router.navigate(['/auth/login']); // Redirigir al login
},
error: (err) => {
if (err.error instanceof ErrorEvent) {
this.errorMessage = `Error: ${err.error.message}`;
const { email, password, confirmPassword } = this.registerForm.value;
if (password === confirmPassword) {
console.log('Register data:', { email, password });
// Aquí iría el llamado al servicio de register con JWT
} else {
// Si el backend devuelve un objeto de error
this.errorMessage = err.error.message || 'Ocurrió un error al registrar el usuario.';
}
console.error('Passwords do not match.');
}
});
}
}
}
......@@ -34,9 +34,6 @@ public class User {
@Enumerated(EnumType.STRING)
private UserStatus status = UserStatus.NO_BOOKINGS;
@Basic(optional = false)
private String password;
@JsonIgnore
@OneToMany(mappedBy = "userId", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Booking> bookings;
......@@ -44,12 +41,11 @@ public class User {
public User() {
}
public User(int id, String name, String email, UserStatus status, List<Booking> bookings, String password) {
public User(int id, String name, String email, UserStatus status, List<Booking> bookings) {
setId(id);
setEmail(email);
setStatus(status);
setBookings(bookings);
setPassword(password);
}
public int getId() {
......@@ -91,12 +87,4 @@ public class User {
public void setBookings(List<Booking> bookings) {
this.bookings = bookings;
}
public String getPassword() {
return this.password;
}
public void setPassword(String password) {
this.password = password;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment