-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthController.java
More file actions
53 lines (44 loc) · 1.9 KB
/
Copy pathAuthController.java
File metadata and controls
53 lines (44 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package ch.fhnw.timerecordingbackend.controller;
import ch.fhnw.timerecordingbackend.dto.authentication.LoginRequest;
import ch.fhnw.timerecordingbackend.dto.authentication.LoginResponse;
import ch.fhnw.timerecordingbackend.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;
import java.util.Map;
@RestController
@RequestMapping("/api/auth")
public class AuthController {
@Autowired
private AuthService authService;
@PostMapping("/login")
public ResponseEntity<?> login(@Valid @RequestBody LoginRequest loginRequest) {
try {
LoginResponse response = authService.login(loginRequest);
return ResponseEntity.ok(response);
} catch (BadCredentialsException e) {
return ResponseEntity
.status(401)
.body(Map.of("error", "Ungültige E-Mail oder Passwort"));
} catch (DisabledException e) {
return ResponseEntity
.status(403)
.body(Map.of("error", "Benutzerkonto ist deaktiviert"));
} catch (Exception e) {
// Detaillierte Fehlerinformation in der Konsole ausgeben
System.err.println("Login-Fehler: " + e.getMessage());
e.printStackTrace();
return ResponseEntity
.status(500)
.body(Map.of("error", "Unbekannter Serverfehler"));
}
}
@PostMapping("/logout")
public ResponseEntity<?> logout(@RequestBody String token) {
authService.logout(token);
return ResponseEntity.ok().body(java.util.Map.of("message", "Erfolgreich ausgeloggt"));
}
}