Skip to content

Commit

Permalink
[feature]/#3/front_history
Browse files Browse the repository at this point in the history
history연동중
  • Loading branch information
khw010419 committed Jun 5, 2024
2 parents 0be89f2 + 6f98bd3 commit 6742ac4
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion server/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ build/
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

.eslintcache


### IntelliJ IDEA ###
Expand Down
1 change: 1 addition & 0 deletions server/src/main/java/com/example/config/DiaryConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public CorsFilter corsFilter() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.setAllowedOrigins(List.of("http://localhost:3000", "http://localhost:8080")); // 개발 중일 때
config.setAllowedOrigins(List.of("http://localhost:3000", "http://localhost:3000/authuser"));
// config.setAllowedOrigins(List.of("https://my-react-app.com")); // 배포 후
config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS"));
config.setAllowedHeaders(List.of("*"));
Expand Down
18 changes: 13 additions & 5 deletions server/src/main/java/com/example/controller/DiaryController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/post")
public class DiaryController {
Expand All @@ -21,30 +25,34 @@ public DiaryController(Diaryservice diaryService, JwtAuthenticationFilter jwtAut
}

@PostMapping("/write_diary")
public ResponseEntity<String> writeDiary(@RequestBody DiaryDto diaryDto, @RequestHeader("Authorization") String authorizationHeader) {
public ResponseEntity<Map<String, String>> writeDiary(@RequestBody DiaryDto diaryDto, @RequestHeader("Authorization") String authorizationHeader) {
// JWT 토큰을 요청 헤더에서 추출
String jwt = extractJwtFromHeader(authorizationHeader);

if (jwt == null) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("JWT 토큰이 없습니다");
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(Collections.singletonMap("message", "JWT 토큰이 없습니다"));
}

// 토큰 검증 및 사용자 정보 추출
if (!jwtAuthenticationFilter.validateToken(jwt)) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("유효하지 않은 JWT 토큰입니다");
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(Collections.singletonMap("message", "유효하지 않은 JWT 토큰입니다"));
}

String loginId = jwtAuthenticationFilter.getUserIdFromJwt(jwt);

if (loginId == null) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("유효하지 않은 JWT 토큰입니다");
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(Collections.singletonMap("message", "유효하지 않은 JWT 토큰입니다"));
}

// 다이어리 DTO에 로그인 ID 설정
diaryDto.setLogin_id(loginId);
diaryService.writeDiary(diaryDto);

return ResponseEntity.status(HttpStatus.CREATED).body("저장이 완료되었습니다");
// 성공 응답 생성
Map<String, String> response = new HashMap<>();
response.put("message", "저장이 완료되었습니다");

return ResponseEntity.status(HttpStatus.CREATED).body(response);
}

// 요청 헤더에서 JWT 토큰을 추출하는 메서드
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.example.controller;
/*package com.example.controller;
import com.example.dto.LoginRequest;
import com.example.domain.entity.Member;
Expand Down Expand Up @@ -64,4 +64,4 @@ public ResponseEntity<String> oauthLoginSuccess() {
return ResponseEntity.ok(message); // 성공 메시지를 반환함
}
}
}*/

0 comments on commit 6742ac4

Please sign in to comment.