content: package net.alex9849.cocktailpi.service;
  
  import net.alex9849.cocktailpi.config.JwtUtils;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.security.authentication.AuthenticationManager;
  import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  import org.springframework.security.core.Authentication;
  import org.springframework.security.core.context.SecurityContextHolder;
  import org.springframework.security.crypto.password.PasswordEncoder;
  import org.springframework.stereotype.Service;
  
  @Service
  public class AuthService {
      @Autowired
      private AuthenticationManager authenticationManager;
  
      @Autowired
      private PasswordEncoder encoder;
  
      @Autowired
      private JwtUtils jwtUtils;
  
      /**
       * Genereates a JSON-Web-Token if the username and the password are corrent.
       * Otherwise an Exception will be thrown
       * @param username The username of the user
       * @param password The password of the user
       * @return A JSON-Web-Token
       */
      public String authUser(String username, String password, boolean remember) {
          Authentication authentication = authenticationManager.authenticate(
                  new UsernamePasswordAuthenticationToken(username, password));
          SecurityContextHolder.getContext().setAuthentication(authentication);
          return jwtUtils.generateJwtToken(authentication, remember);
      }
  
  }
  