Skip to content

Commit

Permalink
feat: Cookie 제거 및 개발용 로그인 개선 (#293)
Browse files Browse the repository at this point in the history
* feat&refactor: Cookie 제거 및 리팩터링 (#282)

* feat: 로컬용 로그인 개선 (#282)

* refactor: IllegalArgumentException 예외 핸들러 메서드 추가 (#282)

* test: 메서드명 수정 (#282)
  • Loading branch information
kdkdhoho authored Sep 2, 2024
1 parent 5e7671e commit 769ab0d
Show file tree
Hide file tree
Showing 22 changed files with 265 additions and 566 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public JwtManager(
public String createAccessToken(Long userId) {
Date now = new Date();
return Jwts.builder()
.header().type("jwt").and()
.header().type("accessToken").and()
.signWith(secretKey)
.issuer(issuer)
.issuedAt(now)
Expand All @@ -61,7 +61,7 @@ public String createAccessToken(Long userId) {
public String createRefreshToken(Long userId) {
Date now = new Date();
return Jwts.builder()
.header().type("jwt").and()
.header().type("refreshToken").and()
.signWith(secretKey)
.issuer(issuer)
.issuedAt(now)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.listywave.auth.application.domain.kakao;

import com.listywave.auth.infra.kakao.KakaoOauthApiClient;
import com.listywave.auth.infra.kakao.response.KakaoLogoutResponse;
import com.listywave.auth.infra.kakao.response.KakaoMember;
import com.listywave.auth.infra.kakao.response.KakaoTokenResponse;
import lombok.RequiredArgsConstructor;
Expand All @@ -13,6 +12,7 @@
@RequiredArgsConstructor
public class KakaoOauthClient {

private static final String TOKEN_PREFIX = "Bearer ";
private final KakaoOauthConfig kakaoOauthConfig;
private final KakaoOauthApiClient apiClient;

Expand All @@ -29,12 +29,11 @@ public KakaoTokenResponse requestToken(String authCode) {
}

public KakaoMember fetchMember(String accessToken) {
return apiClient.fetchKakaoMember("Bearer " + accessToken);
return apiClient.fetchKakaoMember(TOKEN_PREFIX + accessToken);
}

public Long logout(String oauthAccessToken) {
String accessToken = "Bearer " + oauthAccessToken;
KakaoLogoutResponse response = apiClient.logout(accessToken);
return response.id();
public void logout(String oauthAccessToken) {
String accessToken = TOKEN_PREFIX + oauthAccessToken;
apiClient.logout(accessToken);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.listywave.auth.presentation.dto;
package com.listywave.auth.application.dto;

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

Expand All @@ -18,22 +17,7 @@ public record LoginResponse(
String refreshToken
) {

public static LoginResponse of(LoginResult result) {
return LoginResponse.builder()
.id(result.id())
.profileImageUrl(result.profileImageUrl())
.backgroundImageUrl(result.backgroundImageUrl())
.nickname(result.nickname())
.description(result.description())
.followerCount(result.followerCount())
.followingCount(result.followingCount())
.isFirst(result.isFirst())
.accessToken(result.accessToken())
.refreshToken(result.refreshToken())
.build();
}

public static LoginResponse of(User user, String accessToken, String refreshToken) {
public static LoginResponse of(User user, String accessToken, String refreshToken, boolean isFirst) {
return LoginResponse.builder()
.id(user.getId())
.profileImageUrl(user.getProfileImageUrl())
Expand All @@ -42,7 +26,7 @@ public static LoginResponse of(User user, String accessToken, String refreshToke
.description(user.getDescription())
.followerCount(user.getFollowerCount())
.followingCount(user.getFollowingCount())
.isFirst(false)
.isFirst(isFirst)
.accessToken(accessToken)
.refreshToken(refreshToken)
.build();
Expand Down
50 changes: 0 additions & 50 deletions src/main/java/com/listywave/auth/application/dto/LoginResult.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.listywave.auth.presentation.dto;
package com.listywave.auth.application.dto;

public record UpdateTokenResponse(
String accessToken
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
import com.listywave.auth.application.domain.JwtManager;
import com.listywave.auth.application.domain.kakao.KakaoOauthClient;
import com.listywave.auth.application.domain.kakao.KakaoRedirectUriProvider;
import com.listywave.auth.application.dto.LoginResult;
import com.listywave.auth.application.dto.UpdateTokenResult;
import com.listywave.auth.application.dto.LoginResponse;
import com.listywave.auth.application.dto.UpdateTokenResponse;
import com.listywave.auth.infra.kakao.response.KakaoMember;
import com.listywave.auth.infra.kakao.response.KakaoTokenResponse;
import com.listywave.common.exception.CustomException;
Expand Down Expand Up @@ -38,7 +38,7 @@ public String provideRedirectUri() {
return kakaoRedirectUriProvider.provide();
}

public LoginResult login(String authCode) {
public LoginResponse login(String authCode) {
KakaoTokenResponse kakaoTokenResponse = kakaoOauthClient.requestToken(authCode);
KakaoMember kakaoMember = kakaoOauthClient.fetchMember(kakaoTokenResponse.accessToken());

Expand All @@ -51,40 +51,22 @@ public LoginResult login(String authCode) {
);
}

private LoginResult loginNonInit(User user, String kakaoAccessToken) {
private LoginResponse loginNonInit(User user, String kakaoAccessToken) {
if (user.isDelete()) {
throw new CustomException(DELETED_USER_EXCEPTION);
}
user.updateKakaoAccessToken(kakaoAccessToken);
String accessToken = jwtManager.createAccessToken(user.getId());
String refreshToken = jwtManager.createRefreshToken(user.getId());
return LoginResult.of(
user,
false,
accessToken,
refreshToken,
jwtManager.getAccessTokenValidTimeDuration(),
jwtManager.getRefreshTokenValidTimeDuration(),
jwtManager.getAccessTokenValidTimeUnit(),
jwtManager.getRefreshTokenValidTimeUnit()
);
return LoginResponse.of(user, accessToken, refreshToken, false);
}

private LoginResult loginInit(Long kakaoId, String kakaoEmail, String kakaoAccessToken) {
private LoginResponse loginInit(Long kakaoId, String kakaoEmail, String kakaoAccessToken) {
User user = User.init(kakaoId, kakaoEmail, kakaoAccessToken);
User createdUser = userRepository.save(user);
userRepository.save(user);
String accessToken = jwtManager.createAccessToken(user.getId());
String refreshToken = jwtManager.createRefreshToken(user.getId());
return LoginResult.of(
createdUser,
true,
accessToken,
refreshToken,
jwtManager.getAccessTokenValidTimeDuration(),
jwtManager.getRefreshTokenValidTimeDuration(),
jwtManager.getAccessTokenValidTimeUnit(),
jwtManager.getRefreshTokenValidTimeUnit()
);
return LoginResponse.of(user, accessToken, refreshToken, true);
}

public void logout(Long userId) {
Expand All @@ -95,19 +77,10 @@ public void logout(Long userId) {
}

@Transactional(readOnly = true)
public UpdateTokenResult updateToken(Long userId) {
public UpdateTokenResponse updateToken(Long userId) {
User user = userRepository.getById(userId);

String accessToken = jwtManager.createAccessToken(user.getId());
String newRefreshToken = jwtManager.createRefreshToken(user.getId());
return new UpdateTokenResult(
accessToken,
newRefreshToken,
jwtManager.getAccessTokenValidTimeDuration(),
jwtManager.getRefreshTokenValidTimeDuration(),
jwtManager.getAccessTokenValidTimeUnit(),
jwtManager.getRefreshTokenValidTimeUnit()
);
return new UpdateTokenResponse(accessToken);
}

public void withdraw(Long userId) {
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/com/listywave/auth/dev/domain/DevAccount.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.listywave.auth.dev.domain;

import static jakarta.persistence.FetchType.LAZY;

import com.listywave.user.application.domain.User;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.MapsId;
import jakarta.persistence.OneToOne;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class DevAccount {

@Id
@Column(name = "user_id", nullable = false, unique = true)
private Long id;

@MapsId
@OneToOne(fetch = LAZY)
@JoinColumn(name = "user_id", nullable = false, unique = true)
private User user;

@Column(nullable = false, unique = true)
private String account;

@Column(nullable = false)
private String password;

public void validatePassword(String password) {
if (this.password.equals(password)) {
return;
}
throw new IllegalArgumentException("비밀번호가 틀렸습니다.");
}

public Long getUserId() {
return user.getId();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.listywave.auth.dev.presentation;

import com.listywave.auth.application.domain.JwtManager;
import com.listywave.auth.application.dto.LoginResponse;
import com.listywave.auth.dev.domain.DevAccount;
import com.listywave.auth.dev.repository.DevAccountRepository;
import com.listywave.user.application.domain.User;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Profile;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

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

private final JwtManager jwtManager;
private final DevAccountRepository devAccountRepository;

@PostMapping("/login/local")
ResponseEntity<LoginResponse> localLogin(@RequestBody LocalLoginRequest request) {
String account = request.account();
String password = request.password();

Optional<DevAccount> optional = devAccountRepository.findByAccount(account);
DevAccount devAccount = optional.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 계정입니다."));

devAccount.validatePassword(password);
User user = devAccount.getUser();
String accessToken = jwtManager.createAccessToken(user.getId());
String refreshToken = jwtManager.createRefreshToken(user.getId());
LoginResponse response = LoginResponse.of(user, accessToken, refreshToken, false);
return ResponseEntity.ok(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.listywave.auth.dev.presentation;

public record LocalLoginRequest(
String account,
String password
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.listywave.auth.dev.repository;

import com.listywave.auth.dev.domain.DevAccount;
import java.util.Optional;
import org.springframework.context.annotation.Profile;
import org.springframework.data.jpa.repository.JpaRepository;

@Profile("!prod")
public interface DevAccountRepository extends JpaRepository<DevAccount, Long> {

Optional<DevAccount> findByAccount(String account);
}
Loading

0 comments on commit 769ab0d

Please sign in to comment.