Skip to content

Commit

Permalink
added fullname in jwt
Browse files Browse the repository at this point in the history
  • Loading branch information
tofiksa committed Jul 24, 2024
1 parent 22498f6 commit 47f0b4a
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
8 changes: 8 additions & 0 deletions src/main/java/no/josefushighscore/dto/UserDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -47,6 +51,10 @@ public static User builder() {
return new User();
}

public String getFullname() {
return firstname + " " + lastname;
}

public String getUsername() {
return username;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/no/josefushighscore/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class User implements UserDetails {
private String username;

@NotEmpty
@Column(name="`password`", unique = true)
private String password;

@NotEmpty
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/no/josefushighscore/register/UserRegister.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,7 +13,7 @@
public interface UserRegister extends JpaRepository<User,Long> {
@Query("SELECT NEW no.josefushighscore.dto.UserDto(u.username, u.firstname, u.lastname, u.email) " +
"FROM User u WHERE u.username = :username")
Optional<UserDto> getUserDetails(@Param("username") String username);
Optional<User> getUserDetails(@Param("username") String username);
Optional<User> findByUsername(String username);
Optional<User> findByEmail(String email);
}
6 changes: 5 additions & 1 deletion src/main/java/no/josefushighscore/service/JwtService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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());
}
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/no/josefushighscore/service/UserService.java
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -16,7 +16,8 @@ public UserService(UserRegister userRepository) {
this.userRepository = userRepository;
}

public Optional<UserDto> getUserDetails(String username) {
public Optional<User> getUserDetails(String username) {

return this.userRepository.getUserDetails(username);
}
}

0 comments on commit 47f0b4a

Please sign in to comment.