content: package com.codesoom.demo.utils;
  
  import com.codesoom.demo.errors.InvalidTokenException;
  import io.jsonwebtoken.Claims;
  import io.jsonwebtoken.InvalidClaimException;
  import io.jsonwebtoken.Jws;
  import io.jsonwebtoken.Jwt;
  import io.jsonwebtoken.Jwts;
  import io.jsonwebtoken.security.Keys;
  import io.jsonwebtoken.security.SignatureException;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.stereotype.Component;
  
  import java.security.Key;
  
  @Component
  public class JwtUtil {
      private final Key key;
  
      public JwtUtil(@Value("${jwt.secret}") String secret) {
          key = Keys.hmacShaKeyFor(secret.getBytes());
      }
  
      public String encode(Long userId) {
          return Jwts.builder()
                  .claim("userId", userId)
                  .signWith(key)
                  .compact();
      }
  
      public Claims decode(String token) {
          if (token == null || token.isBlank()) {
              throw new InvalidTokenException(token);
          }
  
          try {
              return Jwts.parserBuilder()
                      .setSigningKey(key)
                      .build()
                      .parseClaimsJws(token)
                      .getBody();
          } catch (SignatureException e) {
              throw new InvalidTokenException(token);
          }
      }
  }
  