Bartha Maxime 017235cccf
All checks were successful
Build and test backend / Build-backend (pull_request) Successful in 2m8s
Build and test backend / Test-backend (pull_request) Successful in 2m0s
Build and test FrontEnd / Build-frontend (pull_request) Successful in 24s
fixed inscription in the backend
2024-03-15 17:29:45 +01:00

52 lines
2.0 KiB
Java

package ovh.herisson.Clyde.EndPoints;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import ovh.herisson.Clyde.Responses.UnauthorizedResponse;
import ovh.herisson.Clyde.Services.AuthenticatorService;
import ovh.herisson.Clyde.Tables.InscriptionRequest;
import java.util.Date;
@RestController
@CrossOrigin(originPatterns = "*", allowCredentials = "true")
public class LoginController {
private final AuthenticatorService authServ;
static public class RequestLogin{
private final String identifier;
private final String password;
@JsonFormat(pattern="yyyy-MM-dd'T'HH:mm:ss")
private final Date expirationDate;
public RequestLogin(String identifier, String password, Date expirationDate){
this.identifier = identifier;
this.password = password;
this.expirationDate = expirationDate;
}
}
public LoginController(AuthenticatorService authServ){
this.authServ = authServ;
}
@PostMapping(value = "/login")
public ResponseEntity<String> login(@RequestBody RequestLogin requestLogin){
String sessionToken = authServ.login(requestLogin.identifier,requestLogin.password,requestLogin.expirationDate);
if (sessionToken == null){
return new UnauthorizedResponse<>("Identifier or Password incorrect");
}
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("Set-Cookie",String.format("session_token=%s",sessionToken));
return ResponseEntity.ok().headers(responseHeaders).build();
}
@PostMapping("/request/register")
public ResponseEntity<String> register(@RequestBody InscriptionRequest inscriptionRequest){
authServ.register(inscriptionRequest);
return new ResponseEntity<>("Is OK", HttpStatus.OK);
}
}