diff --git a/src/main/java/no/josefushighscore/dto/UserDto.java b/src/main/java/no/josefushighscore/dto/UserDto.java index ccf07d9..160ebeb 100644 --- a/src/main/java/no/josefushighscore/dto/UserDto.java +++ b/src/main/java/no/josefushighscore/dto/UserDto.java @@ -18,10 +18,14 @@ public class UserDto { @JsonProperty("lastname") private String lastname; + @NotBlank @JsonProperty("email") private String email; + @JsonProperty("fullname") + private String fullname; + public UserDto(String username, String password, String firstname, String lastname, String email) { @@ -47,6 +51,10 @@ public static User builder() { return new User(); } + public String getFullname() { + return firstname + " " + lastname; + } + public String getUsername() { return username; } diff --git a/src/main/java/no/josefushighscore/model/User.java b/src/main/java/no/josefushighscore/model/User.java index 1c96526..12da4cd 100644 --- a/src/main/java/no/josefushighscore/model/User.java +++ b/src/main/java/no/josefushighscore/model/User.java @@ -33,6 +33,7 @@ public class User implements UserDetails { private String username; @NotEmpty + @Column(name="`password`", unique = true) private String password; @NotEmpty diff --git a/src/main/java/no/josefushighscore/register/UserRegister.java b/src/main/java/no/josefushighscore/register/UserRegister.java index adbf3e8..75f169f 100644 --- a/src/main/java/no/josefushighscore/register/UserRegister.java +++ b/src/main/java/no/josefushighscore/register/UserRegister.java @@ -1,7 +1,6 @@ package no.josefushighscore.register; -import no.josefushighscore.dto.UserDto; import no.josefushighscore.model.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; @@ -14,7 +13,7 @@ public interface UserRegister extends JpaRepository { @Query("SELECT NEW no.josefushighscore.dto.UserDto(u.username, u.firstname, u.lastname, u.email) " + "FROM User u WHERE u.username = :username") - Optional getUserDetails(@Param("username") String username); + Optional getUserDetails(@Param("username") String username); Optional findByUsername(String username); Optional findByEmail(String email); } diff --git a/src/main/java/no/josefushighscore/service/JwtService.java b/src/main/java/no/josefushighscore/service/JwtService.java index 692070e..292f46e 100644 --- a/src/main/java/no/josefushighscore/service/JwtService.java +++ b/src/main/java/no/josefushighscore/service/JwtService.java @@ -63,7 +63,7 @@ private String buildToken( .claim("roles",userRegister.findByUsername(userDetails.getUsername()).orElseThrow( () -> new UsernameNotFoundException("Username " + userDetails.getUsername() + " not found")).getRoles()) .claim("email", userRegister.findByUsername(userDetails.getUsername()).orElseThrow( () -> new UsernameNotFoundException("Username " + userDetails.getUsername() + " not found")).getEmail()) - .claim("name", userRegister.findByUsername(userDetails.getUsername()).orElseThrow( () -> new UsernameNotFoundException("Username " + userDetails.getUsername() + " not found")).getFirstname()) + .claim("name", getFullname(userDetails)) .setIssuedAt(new Date(System.currentTimeMillis())) .setExpiration(new Date(System.currentTimeMillis() + expiration)) .signWith(getSignInKey(), SignatureAlgorithm.HS256) @@ -75,6 +75,10 @@ public boolean isTokenValid(String token, UserDetails userDetails) { return (username.equals(userDetails.getUsername())) && !isTokenExpired(token); } + private String getFullname (UserDetails userDetails) { + return userRegister.findByUsername(userDetails.getUsername()).orElseThrow( () -> new UsernameNotFoundException("Username " + userDetails.getUsername() + " not found")).getFirstname() + " " + userRegister.findByUsername(userDetails.getUsername()).orElseThrow( () -> new UsernameNotFoundException("Username " + userDetails.getUsername() + " not found")).getLastname(); + } + private boolean isTokenExpired(String token) { return extractExpiration(token).before(new Date()); } diff --git a/src/main/java/no/josefushighscore/service/UserService.java b/src/main/java/no/josefushighscore/service/UserService.java index 994966d..3148084 100644 --- a/src/main/java/no/josefushighscore/service/UserService.java +++ b/src/main/java/no/josefushighscore/service/UserService.java @@ -1,6 +1,6 @@ package no.josefushighscore.service; -import no.josefushighscore.dto.UserDto; +import no.josefushighscore.model.User; import no.josefushighscore.register.UserRegister; import org.springframework.stereotype.Service; @@ -16,7 +16,8 @@ public UserService(UserRegister userRepository) { this.userRepository = userRepository; } - public Optional getUserDetails(String username) { + public Optional getUserDetails(String username) { + return this.userRepository.getUserDetails(username); } } \ No newline at end of file