Skip to content

Commit

Permalink
Merge pull request #90 from everymeals/feature/store-post-review
Browse files Browse the repository at this point in the history
feature : 개발전용 로그인 API 개발
  • Loading branch information
Qbeom0925 authored Feb 7, 2024
2 parents 9975852 + f500071 commit 62b79f3
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 3 deletions.
4 changes: 3 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ sonar {
property 'sonar.language', 'java'
property 'sonar.sourceEncoding', 'UTF-8'
property("sonar.test.inclusions", "**/*Test.java")
property "sonar.exclusions", "**/test/**, **/*Application*.java, **/dto/**, **/entity/**, **/*Exception*.java, **/*RepositoryImpl.java, **/global/**, **/resources/**, **/*Dao*.java"
property "sonar.exclusions", "**/test/**, **/*Application*.java, **/dto/**, **/entity/**, **/*Exception*.java, **/*RepositoryImpl.java, **/global/**, **/resources/**, **/*Dao*.java, **/dev/**"
property "sonar.java.coveragePlugin", "jacoco"
property 'sonar.coverage.jacoco.xmlReportPaths', 'build/reports/jacoco/test/jacocoTestReport.xml'
}
Expand Down Expand Up @@ -126,6 +126,7 @@ jacocoTestReport {
"**/repository/**",
"**/global/*",
"**/*Dao*",
"**/dev/**",
])
}))
}
Expand Down Expand Up @@ -160,6 +161,7 @@ jacocoTestCoverageVerification {
"**/repository/**",
"**/global/*",
"**/*Dao*",
"**/dev/**",
])
}))
}
Expand Down
51 changes: 51 additions & 0 deletions src/main/java/everymeal/server/dev/controller/DevController.java
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()));
}
}
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 src/main/java/everymeal/server/dev/service/DevService.java
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);
}
}
4 changes: 2 additions & 2 deletions src/main/java/everymeal/server/global/util/JwtUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ public String generateAccessToken(Long idx) {
.compact();
}

public String generateRefreshToken(Long idx, String refreshToken) {
public String generateRefreshToken(Long idx, String accessToken) {
Date now = new Date();
Date expiryDate = new Date(now.getTime() + refreshTokenExpirationMs);
Map<String, Object> claims = new HashMap<>();
claims.put("CLAIM_KEY_IDX", idx);
claims.put("CLAIM_KEY_ACCESS_TOKEN", refreshToken);
claims.put("CLAIM_KEY_ACCESS_TOKEN", accessToken);
return Jwts.builder()
.setClaims(claims)
.setIssuedAt(now)
Expand Down

0 comments on commit 62b79f3

Please sign in to comment.