Skip to content

Commit

Permalink
[FEATURE] 로그인 API 스펙 변경 (#105)
Browse files Browse the repository at this point in the history
* delete: PermissionCheck 어노테이션 (#104)

* delete: 사용자 id 조회 API (#104)

* delete: actuator 권한 수정 (#104)

* feat: 로그인 API 스펙 변경 (#104)

* delete: 내부 식별자 필드 삭제 (#104)
  • Loading branch information
hyunmin0317 authored Nov 30, 2024
1 parent b5d9bcc commit ebaca00
Show file tree
Hide file tree
Showing 8 changed files with 8 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

@Builder
public record LoginResponseDto(
Long memberId,
String username,
MemberRole memberRole,
String accessToken,
String refreshToken
) {

public static LoginResponseDto of(Long memberId, MemberRole memberRole, String accessToken, String refreshToken) {
public static LoginResponseDto of(String username, MemberRole memberRole, String accessToken, String refreshToken) {
return LoginResponseDto.builder()
.memberId(memberId)
.username(username)
.memberRole(memberRole)
.accessToken(accessToken)
.refreshToken(refreshToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@

@Builder
public record RegisterResponseDto(
Long id,
String username,
MemberRole role,
LocalDateTime createdAt
) {

public static RegisterResponseDto from(Member member) {
return RegisterResponseDto.builder()
.id(member.getId())
.username(member.getUsername())
.role(member.getRole())
.createdAt(member.getCreatedAt())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public LoginResponseDto login(LoginRequestDto requestDto) {
Member member = memberRepository.findByUsername(requestDto.username())
.orElseThrow(() -> new GeneralException(ErrorCode.ACCOUNT_NOT_FOUND));
checkPassword(requestDto.password(), member.getPassword());
return generateToken(member.getId(), member.getRole());
return generateToken(member.getUsername(), member.getId(), member.getRole());
}

public LoginResponseDto refresh(RefreshRequestDto requestDto) {
Expand All @@ -54,14 +54,14 @@ public LoginResponseDto refresh(RefreshRequestDto requestDto) {
Member member = memberRepository.findById(oldRefreshToken.getMemberId())
.orElseThrow(() -> new GeneralException(ErrorCode.ACCOUNT_NOT_FOUND));
refreshTokenService.deleteRefreshToken(oldRefreshToken.getToken());
return generateToken(member.getId(), member.getRole());
return generateToken(member.getUsername(), member.getId(), member.getRole());
}

private LoginResponseDto generateToken(Long memberId, MemberRole memberRole) {
private LoginResponseDto generateToken(String username, Long memberId, MemberRole memberRole) {
String accessToken = jwtTokenProvider.createAccessToken(memberId, memberRole, false);
String refreshToken = jwtTokenProvider.createAccessToken(memberId, memberRole, true);
refreshTokenService.saveRefreshToken(memberId, refreshToken);
return LoginResponseDto.of(memberId, memberRole, accessToken, refreshToken);
return LoginResponseDto.of(username, memberRole, accessToken, refreshToken);
}

private void validateUser(String memberName, String username) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.smunity.server.domain.member.service.MemberQueryService;
import com.smunity.server.global.security.annotation.AuthMember;
import com.smunity.server.global.security.annotation.AuthVerified;
import com.smunity.server.global.validation.annotation.PermissionCheck;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
Expand All @@ -32,12 +31,6 @@ public ResponseEntity<Page<MemberResponseDto>> readMembers(@ParameterObject Page
return ResponseEntity.ok(responseDtoPage);
}

@GetMapping("/{id}")
public ResponseEntity<MemberInfoResponseDto> readMember(@PermissionCheck @PathVariable Long id) {
MemberInfoResponseDto responseDto = memberQueryService.readMember(id);
return ResponseEntity.ok(responseDto);
}

@GetMapping("/me")
public ResponseEntity<MemberInfoResponseDto> readMemberInfo(@AuthMember Long memberId) {
MemberInfoResponseDto responseDto = memberQueryService.readMember(memberId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

@Builder
public record MemberInfoResponseDto(
Long id,
String username,
String name,
String department,
Expand All @@ -14,7 +13,6 @@ public record MemberInfoResponseDto(

public static MemberInfoResponseDto from(Member member) {
return MemberInfoResponseDto.builder()
.id(member.getId())
.username(member.getUsername())
.name(member.getName())
.department(member.getDepartment().getName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.requestMatchers(HttpMethod.GET, "/api/v1/questions/**").permitAll()

// 관리자 권한을 가진 사용자 (ROLE_ADMIN)
.requestMatchers("/api/v1/members", "/api/v1/questions/{questionId}/answer", "/actuator/**").hasRole("ADMIN")
.requestMatchers("/api/v1/members", "/api/v1/questions/{questionId}/answer").hasRole("ADMIN")

// 인증된 사용자 (ROLE_USER, ROLE_ADMIN)
.anyRequest().authenticated()
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit ebaca00

Please sign in to comment.