Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Cookie 제거 및 개발용 로그인 개선 #293

Merged
merged 4 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
Comment on lines +20 to +47
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오우 개발용 계정을 관리하는 테이블이 추가되는거군요

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

개발용 DB에만 추가할 예정입니다!

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);
kdkdhoho marked this conversation as resolved.
Show resolved Hide resolved
}
}
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
Loading