content: package com.codesoom.demo.application;
  
  import com.codesoom.demo.domain.Role;
  import com.codesoom.demo.domain.RoleRepository;
  import com.codesoom.demo.domain.User;
  import com.codesoom.demo.domain.UserRepository;
  import com.codesoom.demo.errors.LoginFailException;
  import com.codesoom.demo.utils.JwtUtil;
  import io.jsonwebtoken.Claims;
  import org.springframework.security.crypto.password.PasswordEncoder;
  import org.springframework.stereotype.Service;
  
  import java.util.List;
  
  @Service
  public class AuthenticationService {
      private final UserRepository userRepository;
      private final RoleRepository roleRepository;
      private final JwtUtil jwtUtil;
      private final PasswordEncoder passwordEncoder;
  
      public AuthenticationService(UserRepository userRepository,
                                   RoleRepository roleRepository,
                                   JwtUtil jwtUtil,
                                   PasswordEncoder passwordEncoder) {
          this.userRepository = userRepository;
          this.roleRepository = roleRepository;
          this.jwtUtil = jwtUtil;
          this.passwordEncoder = passwordEncoder;
      }
  
      public String login(String email, String password) {
          User user = userRepository.findByEmail(email)
                  .orElseThrow(() -> new LoginFailException(email));
  
          if (!user.authenticate(password, passwordEncoder)) {
              throw new LoginFailException(email);
          }
  
          return jwtUtil.encode(user.getId());
      }
  
      public Long parseToken(String accessToken) {
          Claims claims = jwtUtil.decode(accessToken);
          return claims.get("userId", Long.class);
      }
  
      public List<Role> roles(Long userId) {
          return roleRepository.findAllByUserId(userId);
      }
  }
  