package net.Broken.Api.Controllers; import net.Broken.Api.Data.Login; import net.Broken.Api.Security.Services.JwtService; import net.Broken.DB.Entity.UserEntity; import net.Broken.DB.Repository.UserRepository; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.security.authentication.AuthenticationManager; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.core.Authentication; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; import java.util.Optional; @RestController @RequestMapping("/api/v2") @CrossOrigin(origins = "*", maxAge = 3600) public class AuthController { private final AuthenticationManager authenticationManager; private final UserRepository userRepository; private final JwtService jwtService; public AuthController(AuthenticationManager authenticationManager, UserRepository userRepository, JwtService jwtService) { this.authenticationManager = authenticationManager; this.userRepository = userRepository; this.jwtService = jwtService; } @PostMapping("login/discord") public String loginDiscord(@Validated @RequestBody Login login) { Authentication authentication = authenticationManager.authenticate( new UsernamePasswordAuthenticationToken(login.redirectUri(), login.code()) ); authentication.getPrincipal(); return "Hello User"; } @GetMapping("login/discord") public String helloUsertest() { Optional user = userRepository.findById(5); return jwtService.buildJwt(user.get()); } @RequestMapping( value = "/**", method = RequestMethod.OPTIONS ) public ResponseEntity handle() { return new ResponseEntity(HttpStatus.OK); } }