54 lines
1.6 KiB
Java
54 lines
1.6 KiB
Java
|
|
package com.ga.api.auth;
|
||
|
|
|
||
|
|
import com.ga.common.model.ApiResponse;
|
||
|
|
import com.ga.core.vo.user.UserResp;
|
||
|
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
||
|
|
import jakarta.servlet.http.HttpServletRequest;
|
||
|
|
import jakarta.validation.Valid;
|
||
|
|
import lombok.RequiredArgsConstructor;
|
||
|
|
import org.springframework.web.bind.annotation.*;
|
||
|
|
|
||
|
|
import java.util.Map;
|
||
|
|
|
||
|
|
@Tag(name = "인증")
|
||
|
|
@RestController
|
||
|
|
@RequestMapping("/api/auth")
|
||
|
|
@RequiredArgsConstructor
|
||
|
|
public class AuthController {
|
||
|
|
|
||
|
|
private final AuthService authService;
|
||
|
|
|
||
|
|
@PostMapping("/login")
|
||
|
|
public ApiResponse<LoginResp> login(@Valid @RequestBody LoginReq req, HttpServletRequest http) {
|
||
|
|
return ApiResponse.ok(authService.login(req, ipOf(http), http.getHeader("User-Agent")));
|
||
|
|
}
|
||
|
|
|
||
|
|
@PostMapping("/refresh")
|
||
|
|
public ApiResponse<LoginResp> refresh(@RequestBody Map<String, String> body) {
|
||
|
|
return ApiResponse.ok(authService.refresh(body.get("refreshToken")));
|
||
|
|
}
|
||
|
|
|
||
|
|
@GetMapping("/me")
|
||
|
|
public ApiResponse<UserResp> me() {
|
||
|
|
return ApiResponse.ok(authService.me());
|
||
|
|
}
|
||
|
|
|
||
|
|
@PostMapping("/password")
|
||
|
|
public ApiResponse<Void> changePassword(@RequestBody Map<String, String> body) {
|
||
|
|
authService.changePassword(body.get("oldPassword"), body.get("newPassword"));
|
||
|
|
return ApiResponse.ok();
|
||
|
|
}
|
||
|
|
|
||
|
|
@PostMapping("/logout")
|
||
|
|
public ApiResponse<Void> logout() {
|
||
|
|
authService.logout();
|
||
|
|
return ApiResponse.ok();
|
||
|
|
}
|
||
|
|
|
||
|
|
private String ipOf(HttpServletRequest req) {
|
||
|
|
String xf = req.getHeader("X-Forwarded-For");
|
||
|
|
if (xf != null && !xf.isBlank()) return xf.split(",")[0].trim();
|
||
|
|
return req.getRemoteAddr();
|
||
|
|
}
|
||
|
|
}
|