This commit is contained in:
Akito123321 2024-06-17 21:14:16 +02:00
parent 379a398bc1
commit 5b64559933
2 changed files with 16 additions and 2 deletions

View File

@ -7,6 +7,7 @@ import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@ -37,7 +38,18 @@ public class AuthenticationController {
if (token == null) if (token == null)
return ResponseEntity.internalServerError().build(); return ResponseEntity.internalServerError().build();
return ResponseEntity.ok(new AuthenticationResponse(user.getUsername(), user.getDisplayName(), token)); return ResponseEntity.ok(new AuthenticationResponse(user, token));
}
@GetMapping(value = "/user", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<StratsUser> getLoggedInUser() {
return ResponseEntity.ok(getUser());
}
private StratsUser getUser() {
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
StratsUser user = (StratsUser) auth.getPrincipal();
return user;
} }
} }

View File

@ -1,5 +1,7 @@
package me.akito123321.valoStrats.rest.requests; package me.akito123321.valoStrats.rest.requests;
public record AuthenticationResponse(String username, String displayName, String token) { import me.akito123321.valoStrats.schemas.StratsUser;
public record AuthenticationResponse(StratsUser user, String token) {
} }