content: package com.taskmanager.taskmanager.security.service;
  
  import java.util.Date;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.security.core.userdetails.UsernameNotFoundException;
  import org.springframework.stereotype.Service;
  
  import com.taskmanager.taskmanager.entity.User;
  import com.taskmanager.taskmanager.repository.UserRepository;
  
  import io.jsonwebtoken.Claims;
  import io.jsonwebtoken.Jwts;
  import io.jsonwebtoken.SignatureAlgorithm;
  
  @Service
  public class JwtService {
  
      @Value("${jwt.secret}")
      private String secret;
  
      @Value("${jwt.expiration}")
      private Long expiration;
  
      @Autowired
      private UserRepository userRepository;
  
      public String generateToken(String username) {
          User actualUser = userRepository.findByEmail(username)
              .orElseThrow(() -> new UsernameNotFoundException(username));
          return Jwts.builder()
                  .setSubject(username)
                  .claim("id", actualUser.getId())
                  .setIssuedAt(new Date())
                  .setExpiration(new Date(System.currentTimeMillis() + expiration))
                  .signWith(SignatureAlgorithm.HS512, secret)
                  .compact();
      }
  
      public Claims getClaimsFromToken(String token) {
          return Jwts.parser()
                  .setSigningKey(secret)
                  .build()
                  .parseClaimsJws(token)
                  .getBody();
      }
  
      public Boolean isTokenValid(String token) {
          try {
              Claims claims = getClaimsFromToken(token);
              return !claims.getExpiration().before(new Date());
          } catch (Exception e) {
              return false;
          }
      }
  
      public String getUsernameFromToken(String token) {
          return getClaimsFromToken(token).getSubject();
      }
  }