Skip to content

Commit

Permalink
feat: 로컬용 로그인 API 구현 (#262) (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdkdhoho committed Jun 18, 2024
1 parent 826b74a commit 3824af5
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.listywave.auth.presentation;

import static com.listywave.common.exception.ErrorCode.INVALID_ACCESS;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.springframework.http.HttpHeaders.SET_COOKIE;

import com.listywave.auth.application.domain.JwtManager;
import com.listywave.auth.presentation.dto.LoginResponse;
import com.listywave.common.exception.CustomException;
import com.listywave.common.util.TimeUtils;
import com.listywave.user.application.domain.User;
import com.listywave.user.repository.user.UserRepository;
import java.time.Duration;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.http.ResponseCookie;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Profile("!prod")
@RequiredArgsConstructor
public class LocalAuthController {

private final UserRepository userRepository;
private final JwtManager jwtManager;

@Value("${local-login.id}")
private String id;
@Value("${local-login.password}")
private String password;

@GetMapping("/login/local")
ResponseEntity<LoginResponse> localLogin(
@RequestParam(name = "id") String id,
@RequestParam(name = "password") String password
) {
if (this.id.equals(id) && this.password.equals(password)) {
User user = userRepository.getById(1L);

String accessToken = jwtManager.createAccessToken(user.getId());
String refreshToken = jwtManager.createRefreshToken(user.getId());

ResponseCookie accessTokenCookie = createCookie(
"accessToken",
accessToken,
Duration.ofSeconds(TimeUtils.convertTimeUnit(
jwtManager.getAccessTokenValidTimeDuration(),
jwtManager.getAccessTokenValidTimeUnit(),
SECONDS
))
);
ResponseCookie refreshTokenCookie = createCookie(
"refreshToken",
refreshToken,
Duration.ofSeconds(TimeUtils.convertTimeUnit(
jwtManager.getRefreshTokenValidTimeDuration(),
jwtManager.getRefreshTokenValidTimeUnit(),
SECONDS
))
);

return ResponseEntity.ok()
.header(SET_COOKIE, accessTokenCookie.toString())
.header(SET_COOKIE, refreshTokenCookie.toString())
.body(LoginResponse.of(user, accessToken, refreshToken));
}
throw new CustomException(INVALID_ACCESS);
}

private ResponseCookie createCookie(String name, String value, Duration maxAge) {
return ResponseCookie.from(name)
.value(value)
.maxAge(maxAge)
.domain("dev.api.listywave.com")
.path("/")
.httpOnly(false)
.secure(false)
.sameSite("None")
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.listywave.auth.presentation.dto;

import com.listywave.auth.application.dto.LoginResult;
import com.listywave.user.application.domain.User;
import lombok.Builder;

@Builder
Expand Down Expand Up @@ -31,4 +32,19 @@ public static LoginResponse of(LoginResult result) {
.refreshToken(result.refreshToken())
.build();
}

public static LoginResponse of(User user, String accessToken, String refreshToken) {
return LoginResponse.builder()
.id(user.getId())
.profileImageUrl(user.getProfileImageUrl())
.backgroundImageUrl(user.getBackgroundImageUrl())
.nickname(user.getNickname())
.description(user.getDescription())
.followerCount(user.getFollowerCount())
.followingCount(user.getFollowingCount())
.isFirst(false)
.accessToken(accessToken)
.refreshToken(refreshToken)
.build();
}
}
4 changes: 4 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,7 @@ server:
tomcat:
mbeanregistry:
enabled: true

local-login:
id:
password:
4 changes: 4 additions & 0 deletions src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,7 @@ jwt:
access-token-valid-time-unit: HOURS
refresh-token-valid-time-duration: 1
refresh-token-valid-time-unit: HOURS

local-login:
id:
password:

0 comments on commit 3824af5

Please sign in to comment.