Add email field to user registration and update related methods
This commit is contained in:
parent
1ce78dc4bf
commit
4d8ad412ee
|
|
@ -0,0 +1,20 @@
|
||||||
|
|
||||||
|
package com.ieslamar.GestionInventario;
|
||||||
|
import com.ieslamar.GestionInventario.entities.Role;
|
||||||
|
import com.ieslamar.GestionInventario.services.UserService;
|
||||||
|
import org.springframework.boot.CommandLineRunner;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
public class DataInitializer {
|
||||||
|
|
||||||
|
// @Bean
|
||||||
|
// public CommandLineRunner initData(UserService userService) {
|
||||||
|
// return args -> {
|
||||||
|
// userService.registerUser("admin", "admin123", Role.ADMIN.name(), "admin123@ejemplo.com");
|
||||||
|
// userService.registerUser("user", "user123", Role.USER.name(), "user123@ejemplo.com");
|
||||||
|
// };
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package com.ieslamar.GestionInventario;
|
package com.ieslamar.GestionInventario;
|
||||||
|
|
||||||
|
import com.ieslamar.GestionInventario.controllers.UserController;
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.SpringApplication;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -15,8 +15,8 @@ public class AuthController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/register")
|
@PostMapping("/register")
|
||||||
public ResponseEntity<String> register(@RequestParam String username, @RequestParam String password, @RequestParam String role) {
|
public ResponseEntity<String> register(@RequestParam String username, @RequestParam String password, @RequestParam String role, @RequestParam String mail) {
|
||||||
userService.registerUser(username, password, role);
|
userService.registerUser(username, password, role, mail);
|
||||||
return ResponseEntity.ok("User registered successfully");
|
return ResponseEntity.ok("User registered successfully");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,8 +34,8 @@ public class UserController {
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping("/register")
|
@PostMapping("/register")
|
||||||
public String register(@RequestParam String username, @RequestParam String password, @RequestParam String role) {
|
public String register(@RequestParam String username, @RequestParam String password, @RequestParam String role, @RequestParam String mail) {
|
||||||
userService.registerUser(username, password, role);
|
userService.registerUser(username, password, role, mail);
|
||||||
return "redirect:/login?success"; // Redirige al login tras registrarse
|
return "redirect:/login?success"; // Redirige al login tras registrarse
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,7 @@ public class User {
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
@Column(nullable = false, unique = true) // No se permite usuarios duplicados
|
@Column(nullable = false, unique = true) // No se permite usuarios duplicados
|
||||||
private String username;
|
private String nombre;
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private String password;
|
private String password;
|
||||||
|
|
@ -23,6 +23,10 @@ public class User {
|
||||||
@Enumerated(EnumType.STRING)
|
@Enumerated(EnumType.STRING)
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private Role role; // Rol del usuario (USER o ADMIN)
|
private Role role; // Rol del usuario (USER o ADMIN)
|
||||||
|
|
||||||
|
@Column(nullable = false, unique = true) // No se permite emails duplicados
|
||||||
|
private String mail;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,5 +5,5 @@ import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
public interface UserRepository extends JpaRepository<User, Long> {
|
public interface UserRepository extends JpaRepository<User, Long> {
|
||||||
Optional<User> findByUsername(String username);
|
Optional<User> findByNombre(String username);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,11 +21,11 @@ public class UserDetailsServiceImpl implements UserDetailsService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
|
||||||
User user = userRepository.findByUsername(username)
|
User user = userRepository.findByNombre(username)
|
||||||
.orElseThrow(() -> new UsernameNotFoundException("Usuario no encontrado"));
|
.orElseThrow(() -> new UsernameNotFoundException("Usuario no encontrado"));
|
||||||
|
|
||||||
return new org.springframework.security.core.userdetails.User(
|
return new org.springframework.security.core.userdetails.User(
|
||||||
user.getUsername(),
|
user.getNombre(),
|
||||||
user.getPassword(),
|
user.getPassword(),
|
||||||
Collections.singletonList(new SimpleGrantedAuthority("ROLE_" + user.getRole().name()))
|
Collections.singletonList(new SimpleGrantedAuthority("ROLE_" + user.getRole().name()))
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -17,11 +17,12 @@ public class UserService {
|
||||||
this.passwordEncoder = passwordEncoder;
|
this.passwordEncoder = passwordEncoder;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void registerUser(String username, String password, String role) {
|
public void registerUser(String username, String password, String role, String mail) {
|
||||||
User user = new User();
|
User user = new User();
|
||||||
user.setUsername(username);
|
user.setNombre(username);
|
||||||
user.setPassword(passwordEncoder.encode(password));
|
user.setPassword(passwordEncoder.encode(password));
|
||||||
user.setRole(Role.valueOf(role));
|
user.setRole(Role.valueOf(role));
|
||||||
|
user.setMail(mail);
|
||||||
userRepository.save(user);
|
userRepository.save(user);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||||
|
|
||||||
# Configuración de JPA e Hibernate
|
# Configuración de JPA e Hibernate
|
||||||
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
|
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
|
||||||
spring.jpa.hibernate.ddl-auto=update
|
#spring.jpa.hibernate.ddl-auto=update
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue