src/main/java/hexlet/code/controller/AuthenticationController.java
package hexlet.code.controller;
import hexlet.code.dto.AuthRequest;
import hexlet.code.util.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/login")
public final class AuthenticationController {
@Autowired
private JWTUtils jwtUtils;
@Autowired
private AuthenticationManager authenticationManager;
@PostMapping
@ResponseStatus(HttpStatus.ACCEPTED)
public String authenticate(@RequestBody AuthRequest authRequest) {
var authentication = new UsernamePasswordAuthenticationToken(
authRequest.getUsername(), authRequest.getPassword());
authenticationManager.authenticate(authentication);
var token = jwtUtils.generateToken(authRequest.getUsername());
return token;
}
}