Add: login wrapper to huertos.
This commit is contained in:
@@ -23,14 +23,14 @@ public class HuertosWebClient {
|
||||
this.coreUrl = coreUrl;
|
||||
}
|
||||
|
||||
public String login(String username, String password) {
|
||||
public LoginResponse login(String username, String password) {
|
||||
LoginRequest req = new LoginRequest(username, password, (byte) 1);
|
||||
LoginResponse resp = restTemplate.postForObject(
|
||||
coreUrl + "/auth/login",
|
||||
req,
|
||||
LoginResponse.class
|
||||
);
|
||||
return resp.token();
|
||||
return resp;
|
||||
}
|
||||
|
||||
public UserWithCredentialDto getUserWithCredential(UUID userId, Byte serviceId) {
|
||||
|
||||
@@ -29,7 +29,7 @@ public class HuertosStartup implements ApplicationRunner {
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) {
|
||||
String token = client.login(user, pass);
|
||||
String token = client.login(user, pass).token();
|
||||
tokenHolder.setToken(token);
|
||||
System.out.println("TOKEN recibido: " + token);
|
||||
System.out.println("HUERTOS CLIENT has been authenticated in CORE");
|
||||
|
||||
@@ -42,7 +42,7 @@ public class SecurityConfig {
|
||||
)
|
||||
.authorizeHttpRequests(auth -> auth
|
||||
// PUBLICAS
|
||||
.requestMatchers("/login").permitAll()
|
||||
.requestMatchers("/auth/login").permitAll()
|
||||
.requestMatchers("/announcements").permitAll()
|
||||
.requestMatchers("/requests/mine").permitAll()
|
||||
.requestMatchers("/users/waitlist/limited").permitAll()
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package net.miarma.backend.huertos.controller;
|
||||
|
||||
import net.miarma.backend.huertos.client.HuertosWebClient;
|
||||
import net.miarma.backend.huertos.dto.HuertosLoginResponse;
|
||||
import net.miarma.backend.huertos.dto.HuertosUserMetadataDto;
|
||||
import net.miarma.backend.huertos.mapper.HuertosUserMetadataMapper;
|
||||
import net.miarma.backend.huertos.model.HuertosUserMetadata;
|
||||
import net.miarma.backend.huertos.service.HuertosUserMetadataService;
|
||||
import net.miarma.backlib.dto.LoginRequest;
|
||||
import net.miarma.backlib.dto.LoginResponse;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
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.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/auth")
|
||||
public class HuertosAuthController {
|
||||
private final HuertosUserMetadataService metadataService;
|
||||
private final HuertosWebClient webClient;
|
||||
|
||||
public HuertosAuthController(HuertosUserMetadataService metadataService,
|
||||
HuertosWebClient webClient) {
|
||||
this.metadataService = metadataService;
|
||||
this.webClient = webClient;
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
public ResponseEntity<HuertosLoginResponse> login(@RequestBody LoginRequest req) {
|
||||
LoginResponse coreResponse = webClient.login(req.username(), req.password());
|
||||
HuertosUserMetadata metadata = metadataService.getById(coreResponse.user().getUserId());
|
||||
return ResponseEntity.ok(
|
||||
new HuertosLoginResponse(
|
||||
coreResponse.token(),
|
||||
coreResponse.user(),
|
||||
coreResponse.account(),
|
||||
HuertosUserMetadataMapper.toDto(metadata)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package net.miarma.backend.huertos.dto;
|
||||
|
||||
import net.miarma.backlib.dto.CredentialDto;
|
||||
import net.miarma.backlib.dto.UserDto;
|
||||
|
||||
public record HuertosLoginResponse(
|
||||
String token,
|
||||
UserDto user,
|
||||
CredentialDto account,
|
||||
HuertosUserMetadataDto metadata
|
||||
) {
|
||||
}
|
||||
Reference in New Issue
Block a user