2022-05-19 01:08:47 +02:00
|
|
|
package net.Broken.Api.Controllers;
|
|
|
|
|
|
|
|
import net.Broken.Api.Data.Login;
|
2022-05-21 01:14:50 +02:00
|
|
|
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;
|
2022-05-19 01:08:47 +02:00
|
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
|
|
import org.springframework.security.core.Authentication;
|
2022-05-21 01:14:50 +02:00
|
|
|
import org.springframework.validation.annotation.Validated;
|
2022-05-19 01:08:47 +02:00
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
2022-05-21 01:14:50 +02:00
|
|
|
import java.util.Optional;
|
|
|
|
|
2022-05-19 01:08:47 +02:00
|
|
|
@RestController
|
|
|
|
@RequestMapping("/api/v2")
|
2022-05-21 01:14:50 +02:00
|
|
|
@CrossOrigin(origins = "*", maxAge = 3600)
|
2022-05-19 01:08:47 +02:00
|
|
|
public class AuthController {
|
|
|
|
private final AuthenticationManager authenticationManager;
|
|
|
|
|
2022-05-21 01:14:50 +02:00
|
|
|
private final UserRepository userRepository;
|
|
|
|
|
|
|
|
private final JwtService jwtService;
|
|
|
|
|
|
|
|
public AuthController(AuthenticationManager authenticationManager, UserRepository userRepository, JwtService jwtService) {
|
2022-05-19 01:08:47 +02:00
|
|
|
this.authenticationManager = authenticationManager;
|
2022-05-21 01:14:50 +02:00
|
|
|
this.userRepository = userRepository;
|
|
|
|
this.jwtService = jwtService;
|
2022-05-19 01:08:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@PostMapping("login/discord")
|
2022-05-21 01:14:50 +02:00
|
|
|
public String loginDiscord(@Validated @RequestBody Login login) {
|
2022-05-19 01:08:47 +02:00
|
|
|
Authentication authentication = authenticationManager.authenticate(
|
|
|
|
new UsernamePasswordAuthenticationToken(login.redirectUri(), login.code())
|
|
|
|
);
|
|
|
|
|
2022-05-21 01:14:50 +02:00
|
|
|
authentication.getPrincipal();
|
|
|
|
|
2022-05-19 01:08:47 +02:00
|
|
|
return "Hello User";
|
|
|
|
}
|
|
|
|
|
2022-05-21 01:14:50 +02:00
|
|
|
@GetMapping("login/discord")
|
|
|
|
public String helloUsertest() {
|
|
|
|
Optional<UserEntity> user = userRepository.findById(5);
|
|
|
|
|
|
|
|
return jwtService.buildJwt(user.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
@RequestMapping(
|
|
|
|
value = "/**",
|
|
|
|
method = RequestMethod.OPTIONS
|
|
|
|
)
|
|
|
|
public ResponseEntity handle() {
|
|
|
|
return new ResponseEntity(HttpStatus.OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-05-19 01:08:47 +02:00
|
|
|
|
|
|
|
}
|