-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathAuthController.java
More file actions
136 lines (118 loc) · 5.71 KB
/
AuthController.java
File metadata and controls
136 lines (118 loc) · 5.71 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.fredmaina.chatapp.Auth.controllers;
import com.fredmaina.chatapp.Auth.Dtos.AuthResponse;
import com.fredmaina.chatapp.Auth.Dtos.GoogleOAuthRequest;
import com.fredmaina.chatapp.Auth.Dtos.LoginRequest;
import com.fredmaina.chatapp.Auth.Dtos.SignUpRequest;
import com.fredmaina.chatapp.Auth.Models.User;
import com.fredmaina.chatapp.Auth.Repositories.UserRepository;
import com.fredmaina.chatapp.Auth.services.AuthService;
import com.fredmaina.chatapp.Auth.services.JWTService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@Slf4j
@Controller
@RequestMapping("/api/auth")
public class AuthController {
@Autowired
AuthService authService;
@Autowired
JWTService jwtService;
@Autowired
UserRepository userRepository;
@PostMapping("/login")
public ResponseEntity<AuthResponse> login(@RequestBody LoginRequest loginRequest) {
AuthResponse authResponse = authService.login(loginRequest);
if (authResponse.isSuccess()) {
return ResponseEntity.ok(authResponse);
}
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(authResponse);
}
@PostMapping("/register")
public ResponseEntity<AuthResponse> register(@Valid @RequestBody SignUpRequest signUpRequest) {
AuthResponse authResponse = authService.signUp(signUpRequest);
if (authResponse.isSuccess()) {
return ResponseEntity.status(HttpStatus.CREATED).body(authResponse);
}
if ("Username already exists (case-insensitive)".equals(authResponse.getMessage()) || "Email already exists".equals(authResponse.getMessage())) {
return ResponseEntity.status(HttpStatus.CONFLICT).body(authResponse);
}
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(authResponse); // General bad request for other issues
}
@PostMapping("/oauth/google")
public ResponseEntity<?> googleOAuth(@RequestBody GoogleOAuthRequest request) {
AuthResponse response = authService.handleGoogleOAuth(request.getCode(), request.getRedirectUri());
// log.error(response.toString()); // log.info or log.debug might be more appropriate for successful responses
if (response.isSuccess()) {
log.info("Google OAuth successful for user: {}", response.getUser() != null ? response.getUser().getEmail() : "Unknown");
return ResponseEntity.ok(response);
} else {
log.warn("Google OAuth failed: {}", response.getMessage());
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response);
}
}
@GetMapping("/me")
public ResponseEntity<AuthResponse> me(@RequestHeader("Authorization") String authHeader) {
String token = authHeader.replace("Bearer ", "");
String email = jwtService.getUsernameFromToken(token); // This actually gets the email (subject of token)
User user = userRepository.findByEmail(email).orElse(null);
if (user == null) {
return ResponseEntity.status(HttpStatus.NOT_FOUND)
.body(AuthResponse.builder().success(false).message("User not found for provided token.").build());
}
return ResponseEntity.ok(
AuthResponse.builder()
.success(true)
.token(token) // Consider if sending the token back is necessary here
.user(user)
.build());
}
@PostMapping("/set-username")
public ResponseEntity<AuthResponse> setUsername(@RequestBody Map<String, String> map) {
String email = map.get("email");
String username = map.get("username");
if (email == null || email.isBlank() || username == null || username.isBlank()) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(AuthResponse.builder().success(false).message("Email and username are required.").build());
}
AuthResponse authResponse = authService.setUsername(email, username);
if (authResponse.isSuccess()) {
return ResponseEntity.ok(authResponse);
}
// Distinguish between user not found and username taken
if ("Username already taken (case-insensitive)".equals(authResponse.getMessage())) {
return ResponseEntity.status(HttpStatus.CONFLICT).body(authResponse);
}
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(authResponse); // Assuming "Invalid email" means user not found
}
@GetMapping("/check-username/{username}")
public ResponseEntity<Map<String, Object>> checkUsername(@PathVariable String username) {
try {
boolean exists = authService.checkUsernameExists(username);
if (exists) {
return ResponseEntity.ok(Map.of(
"success", true,
"exists", true,
"username", username
));
} else {
log.error("username {} not found for some weird reason", username);
return ResponseEntity.ok(Map.of(
"success", true,
"exists", false
));
}
} catch (Exception e) {
log.error("Error checking username: {}", username, e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(Map.of(
"success", false,
"message", "Error checking username"
));
}
}
}