fixed to runnable state

This commit is contained in:
Jonas Weissengruber 2023-12-17 17:17:14 +01:00
parent f33d798663
commit baa24b4b86
7 changed files with 14 additions and 13 deletions

View file

@ -1,4 +1,4 @@
package me.jweissen.aeticket.dto.response;
public record UserResponseDto(int id, String firstname, String lastname, String email) {
public record UserResponseDto(Long id, String firstname, String lastname, String email) {
}

View file

@ -16,7 +16,7 @@ public class Cart {
private Long id;
@OneToMany(mappedBy = "cart")
@JoinColumn(nullable = false)
@Column(nullable = false)
private List<CartEntry> cartEntries;
@ManyToOne

View file

@ -33,6 +33,5 @@ public class Category {
private Event event;
@OneToMany(mappedBy = "category")
@JoinColumn
private List<CartEntry> cartEntries;
}

View file

@ -42,6 +42,5 @@ public class User {
private Role role;
@OneToMany(mappedBy = "user")
@JoinColumn(nullable = false)
private List<Cart> carts;
}

View file

@ -4,5 +4,6 @@ import me.jweissen.aeticket.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Integer> {
User findByToken(String token);
User findByEmail(String email);
}

View file

@ -1,6 +1,7 @@
package me.jweissen.aeticket.service;
import me.jweissen.aeticket.dto.response.LoginResponseDto;
import me.jweissen.aeticket.model.Role;
import me.jweissen.aeticket.model.User;
import me.jweissen.aeticket.repository.UserRepository;
import org.springframework.stereotype.Service;
@ -10,12 +11,10 @@ import java.util.Optional;
@Service
public class AuthService {
private final JwtService jwtService;
private final UserService userService;
private final UserRepository userRepository;
public AuthService(JwtService jwtService, UserService userService, UserRepository userRepository) {
public AuthService(JwtService jwtService, UserRepository userRepository) {
this.jwtService = jwtService;
this.userService = userService;
this.userRepository = userRepository;
}
@ -31,5 +30,13 @@ public class AuthService {
return Optional.of(new LoginResponseDto(username));
}
public Optional<Role> getRole(String token) {
User user = userRepository.findByToken(token);
if (user == null) {
return Optional.empty();
}
return Optional.of(user.getRole());
}
}

View file

@ -14,17 +14,12 @@ import java.util.Optional;
@Service
public class JwtService {
@Value("${token.secret}")
private String secret;
@Value("${token.validForHours}")
private Long tokenValidForHours;
private final JWTVerifier jwtVerifier;
private final Algorithm algorithm;
private final String userIdClaimKey = "userId";
private final Long tokenValidForMillis;
public JwtService() {
public JwtService(@Value("${token.secret}") String secret, @Value("${token.validForHours}") Long tokenValidForHours) {
tokenValidForMillis = 1000L * 3600 * tokenValidForHours;
algorithm = Algorithm.HMAC256(secret);
jwtVerifier = JWT.require(algorithm).build();