Skip to content

Commit

Permalink
feat: 로컬용 로그인 개선 (#282)
Browse files Browse the repository at this point in the history
  • Loading branch information
kdkdhoho committed Sep 1, 2024
1 parent 439ebc7 commit 8b8e7a7
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 87 deletions.
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);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ ResponseEntity<ErrorResponse> handleCustomException(CustomException e) {
}

@ExceptionHandler(Exception.class)
protected ResponseEntity<Void> handleException(Exception e) {
protected ResponseEntity<String> handleException(Exception e) {
log.error("[InternalServerError] : {}", e.getMessage(), e);
return ResponseEntity.status(INTERNAL_SERVER_ERROR).build();
return ResponseEntity.status(INTERNAL_SERVER_ERROR).body(e.getMessage());
}

@ExceptionHandler(MethodArgumentTypeMismatchException.class)
Expand Down

0 comments on commit 8b8e7a7

Please sign in to comment.