-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #90 from everymeals/feature/store-post-review
feature : 개발전용 로그인 API 개발
- Loading branch information
Showing
5 changed files
with
105 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
src/main/java/everymeal/server/dev/controller/DevController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package everymeal.server.dev.controller; | ||
|
||
|
||
import everymeal.server.dev.controller.dto.response.LoginRes; | ||
import everymeal.server.dev.service.DevService; | ||
import everymeal.server.global.dto.response.ApplicationResponse; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseCookie; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RequestMapping("/api/v1/devs") | ||
@RestController | ||
@RequiredArgsConstructor | ||
@Tag(name = "dev API", description = "개발 관련 API입니다") | ||
public class DevController { | ||
|
||
private final DevService devService; | ||
|
||
@Operation( | ||
summary = "(개발 전용)유저 로그인", | ||
description = | ||
""" | ||
(개발 전용) 해당 API 운영 단계에서 빠질 예정입니다. <br> | ||
유저 로그인을 진행합니다. <br> | ||
유저 로그인 성공 시, access-token과 refresh-token을 반환합니다. | ||
""") | ||
@GetMapping("/user/login") | ||
public ResponseEntity<ApplicationResponse<LoginRes>> userLogin(@RequestParam String email) { | ||
return setRefreshToken(devService.userLogin(email)); | ||
} | ||
|
||
private ResponseEntity<ApplicationResponse<LoginRes>> setRefreshToken(LoginRes response) { | ||
ResponseCookie cookie = | ||
ResponseCookie.from("refresh-token", response.refreshToken()) | ||
.httpOnly(true) | ||
.sameSite("None") | ||
.path("/") | ||
.maxAge(60 * 60 * 24 * 30L) | ||
// .secure(true) | ||
.build(); | ||
return ResponseEntity.ok() | ||
.header("Set-Cookie", cookie.toString()) | ||
.body(ApplicationResponse.ok(response.withoutRefreshToken())); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/java/everymeal/server/dev/controller/dto/response/LoginRes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package everymeal.server.dev.controller.dto.response; | ||
|
||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.annotation.JsonInclude.Include; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
|
||
public record LoginRes( | ||
String accessToken, | ||
String nickname, | ||
String profileImg, | ||
@JsonInclude(Include.NON_NULL) @Schema(hidden = true) String refreshToken) { | ||
public LoginRes withoutRefreshToken() { | ||
return new LoginRes(accessToken, nickname, profileImg, null); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/everymeal/server/dev/service/DevService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package everymeal.server.dev.service; | ||
|
||
|
||
import everymeal.server.dev.controller.dto.response.LoginRes; | ||
import everymeal.server.global.exception.ApplicationException; | ||
import everymeal.server.global.util.JwtUtil; | ||
import everymeal.server.global.util.aws.S3Util; | ||
import everymeal.server.user.entity.User; | ||
import everymeal.server.user.repository.UserRepository; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class DevService { | ||
|
||
private final UserRepository userRepository; | ||
private final JwtUtil jwtUtil; | ||
|
||
public LoginRes userLogin(String email) { | ||
User user = | ||
userRepository | ||
.findByEmail(email) | ||
.orElseThrow(() -> new ApplicationException("해당 유저가 없습니다.")); | ||
String accessToken = jwtUtil.generateAccessToken(user.getIdx()); | ||
String refreshToken = jwtUtil.generateRefreshToken(user.getIdx(), accessToken); | ||
return new LoginRes( | ||
accessToken, | ||
user.getNickname(), | ||
S3Util.getImgUrl(user.getProfileImgUrl()), | ||
refreshToken); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters