Skip to content

Commit

Permalink
using JWT Token in ProblemService
Browse files Browse the repository at this point in the history
  • Loading branch information
KiSeungMin committed Jul 25, 2024
1 parent 7f1f67e commit da0eb47
Show file tree
Hide file tree
Showing 20 changed files with 100 additions and 435 deletions.
27 changes: 0 additions & 27 deletions src/main/java/com/aisip/OnO/backend/Auth/AuthService.java

This file was deleted.

43 changes: 0 additions & 43 deletions src/main/java/com/aisip/OnO/backend/Auth/AuthUserController.java

This file was deleted.

28 changes: 0 additions & 28 deletions src/main/java/com/aisip/OnO/backend/Auth/GoogleUser.java

This file was deleted.

20 changes: 0 additions & 20 deletions src/main/java/com/aisip/OnO/backend/Auth/UserEntity.java

This file was deleted.

This file was deleted.

27 changes: 0 additions & 27 deletions src/main/java/com/aisip/OnO/backend/Dto/User/UserRegisterDto.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.aisip.OnO.backend.Auth;
package com.aisip.OnO.backend.controller;

import com.aisip.OnO.backend.service.AuthService;
import com.aisip.OnO.backend.Auth.GoogleTokenVerifier;
import com.aisip.OnO.backend.Auth.JwtTokenProvider;
import com.aisip.OnO.backend.entity.User;
import com.google.api.client.googleapis.auth.oauth2.GoogleIdToken;
import com.google.common.io.BaseEncoding;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -27,8 +31,8 @@ public class AuthController {
public ResponseEntity<?> googleLogin(@RequestBody TokenRequest tokenRequest) {
try {
GoogleIdToken.Payload payload = googleTokenVerifier.verifyToken(tokenRequest.getIdToken());
UserEntity userEntity = authService.registerOrLoginUser(payload.getEmail(), (String) payload.get("name"));
String token = jwtTokenProvider.createToken(userEntity.getUserId(), userEntity.getEmail());
User user = authService.registerOrLoginUser(payload.getEmail(), (String) payload.get("name"));
String token = jwtTokenProvider.createToken(user.getUserId(), user.getEmail());
return ResponseEntity.ok(new AuthResponse(token));
} catch (IllegalArgumentException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import com.aisip.OnO.backend.service.ProblemService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.coyote.Response;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;
Expand All @@ -23,10 +23,11 @@ public class ProblemController {

@GetMapping("/problem/{problemId}")
public ResponseEntity<?> getProblemByUserId(
@RequestHeader("userId") Long userId,
Authentication authentication,
@PathVariable("problemId") Long problemId
) {
try {
Long userId = (Long) authentication.getPrincipal();
ProblemResponseDto problemResponseDto = problemService.findProblemByUserId(userId, problemId);
return ResponseEntity.ok(problemResponseDto);
} catch (ProblemNotFoundException e) {
Expand All @@ -35,18 +36,19 @@ public ResponseEntity<?> getProblemByUserId(
}

@GetMapping("/problems")
public ResponseEntity<?> getProblemsByUserId(@RequestHeader("userId") Long userId){
public ResponseEntity<?> getProblemsByUserId(Authentication authentication) {
Long userId = (Long) authentication.getPrincipal();
List<ProblemResponseDto> problems = problemService.findAllProblemsByUserId(userId);
return ResponseEntity.ok(problems);
}

@PostMapping("/problem")
public ResponseEntity<?> registerProblem(
@RequestHeader("userId") Long userId,
Authentication authentication,
@ModelAttribute ProblemRegisterDto problemRegisterDto
) {
try {
System.out.println(problemRegisterDto.toString());
Long userId = (Long) authentication.getPrincipal();
boolean isSaved = problemService.saveProblem(userId, problemRegisterDto);

if(isSaved){
Expand All @@ -62,28 +64,13 @@ public ResponseEntity<?> registerProblem(
}
}

/*
@PutMapping("/problem/{problemId}")
public ResponseEntity<?> updateProblem(
@RequestHeader("userId") Long userId,
@PathVariable("problemId") Long problemId,
@RequestBody ProblemRegisterDto problemRegisterDto
) {
try {
ProblemResponseDto updatedProblem = problemService.updateProblem(userId, problemId, problemRegisterDto);
return ResponseEntity.ok(updatedProblem);
} catch (ProblemNotFoundException e) {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage());
}
}
*/

@DeleteMapping("/problem")
public ResponseEntity<?> deleteProblem(
@RequestHeader("userId") Long userId,
Authentication authentication,
@RequestHeader("problemId") Long problemId
) {
try {
Long userId = (Long) authentication.getPrincipal();
problemService.deleteProblem(userId, problemId);
return ResponseEntity.ok("삭제를 완료했습니다.");
} catch (ProblemNotFoundException e) {
Expand Down
61 changes: 33 additions & 28 deletions src/main/java/com/aisip/OnO/backend/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,45 @@
package com.aisip.OnO.backend.controller;

import com.aisip.OnO.backend.Dto.User.UserRegisterDto;
import com.aisip.OnO.backend.Dto.User.UserResponseDto;
import com.aisip.OnO.backend.service.UserService;
import com.google.auth.oauth2.IdToken;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import com.aisip.OnO.backend.service.AuthService;
import com.aisip.OnO.backend.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequiredArgsConstructor
@RestController
@RequestMapping("/api/user")
public class UserController {

private final UserService userService;
@Autowired
private AuthService authService;

@GetMapping("/{userId}")
public ResponseEntity<UserResponseDto> getUserById(@PathVariable Long userId) {
UserResponseDto userResponseDto = userService.getUserByUserId(userId);
return ResponseEntity.ok().body(userResponseDto);
@GetMapping("/info")
public ResponseEntity<?> getUserInfo(Authentication authentication) {
Long userId = (Long) authentication.getPrincipal();
User user = authService.getUserById(userId);
if (user != null) {
return ResponseEntity.ok(user);
} else {
return ResponseEntity.status(404).body(new ErrorResponse("User not found"));
}
}

@PostMapping("/join")
public ResponseEntity<UserResponseDto> saveUser(@RequestBody UserRegisterDto userRegisterDto) {
UserResponseDto userResponseDto = userService.saveUser(userRegisterDto);
return ResponseEntity.ok().body(userResponseDto);
}
public static class ErrorResponse {
private String error;

public ErrorResponse(String error) {
this.error = error;
}

// @GetMapping("/autoLogin/{googleId}")
// public ResponseEntity<?> autoLogin(@PathVariable String googleId) {
// try {
// UserResponseDto userResponseDto = userService.getUserByGoogleId(googleId);
// return ResponseEntity.ok().body(userResponseDto);
// } catch (UserNotFoundException e) {
// return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage());
// }
// }
}
public String getError() {
return error;
}

public void setError(String error) {
this.error = error;
}
}
}
24 changes: 0 additions & 24 deletions src/main/java/com/aisip/OnO/backend/converter/UserConverter.java

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit da0eb47

Please sign in to comment.