Skip to content

Commit

Permalink
feat: 로그인 API 응답에 memberId와 nickname 필드 추가 (#322)
Browse files Browse the repository at this point in the history
* feat: 로그인 API 응답에 memberId와 nickname 필드 추가

* refactor: 로그인용 dto 분리 및 공통 dto에 prefix로 auth 추가

* feat: valid 어노테이션 추가
  • Loading branch information
ChooSeoyeon committed Oct 11, 2024
1 parent c6a71e9 commit 0c3b932
Show file tree
Hide file tree
Showing 13 changed files with 64 additions and 48 deletions.
2 changes: 1 addition & 1 deletion backend/http/auth.http
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ POST {{base-url}}/auth/signup
Content-Type: application/json

{
"ci": "poke12345678"
"ci": "poke1234567"
}

### 토큰 재발급 API
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.zzang.chongdae.auth.controller;

import com.zzang.chongdae.auth.service.AuthService;
import com.zzang.chongdae.auth.service.dto.AuthInfoDto;
import com.zzang.chongdae.auth.service.dto.AuthTokenDto;
import com.zzang.chongdae.auth.service.dto.LoginRequest;
import com.zzang.chongdae.auth.service.dto.LoginResponse;
import com.zzang.chongdae.auth.service.dto.SignupRequest;
import com.zzang.chongdae.auth.service.dto.SignupResponse;
import com.zzang.chongdae.auth.service.dto.SignupResponseDto;
import com.zzang.chongdae.auth.service.dto.TokenDto;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand All @@ -26,33 +27,34 @@ public class AuthController {
private final CookieConsumer cookieConsumer;

@PostMapping("/auth/login")
public ResponseEntity<Void> login(
public ResponseEntity<LoginResponse> login(
@RequestBody @Valid LoginRequest request, HttpServletResponse servletResponse) {
TokenDto tokenDto = authService.login(request);
addTokenToHttpServletResponse(tokenDto, servletResponse);
return ResponseEntity.ok().build();
AuthInfoDto authInfo = authService.login(request);
addTokenToHttpServletResponse(authInfo.authToken(), servletResponse);
LoginResponse response = new LoginResponse(authInfo.authMember());
return ResponseEntity.ok(response);
}

@PostMapping("/auth/signup")
public ResponseEntity<SignupResponse> signup(
@RequestBody SignupRequest request, HttpServletResponse servletResponse) {
SignupResponseDto output = authService.signup(request);
SignupResponse response = new SignupResponse(output);
addTokenToHttpServletResponse(output.token(), servletResponse);
@RequestBody @Valid SignupRequest request, HttpServletResponse servletResponse) {
AuthInfoDto authInfo = authService.signup(request);
addTokenToHttpServletResponse(authInfo.authToken(), servletResponse);
SignupResponse response = new SignupResponse(authInfo.authMember());
return ResponseEntity.ok(response);
}

@PostMapping("/auth/refresh")
public ResponseEntity<Void> refresh(
HttpServletRequest servletRequest, HttpServletResponse servletResponse) {
String refreshToken = cookieConsumer.getRefreshToken(servletRequest.getCookies());
TokenDto tokenDto = authService.refresh(refreshToken);
addTokenToHttpServletResponse(tokenDto, servletResponse);
AuthTokenDto authToken = authService.refresh(refreshToken);
addTokenToHttpServletResponse(authToken, servletResponse);
return ResponseEntity.ok().build();
}

private void addTokenToHttpServletResponse(TokenDto tokenDto, HttpServletResponse servletResponse) {
List<Cookie> cookies = cookieExtractor.extractAuthCookies(tokenDto);
private void addTokenToHttpServletResponse(AuthTokenDto authToken, HttpServletResponse servletResponse) {
List<Cookie> cookies = cookieExtractor.extractAuthCookies(authToken);
cookieConsumer.addCookies(servletResponse, cookies);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.zzang.chongdae.auth.controller;

import com.zzang.chongdae.auth.service.dto.TokenDto;
import com.zzang.chongdae.auth.service.dto.AuthTokenDto;
import jakarta.servlet.http.Cookie;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -12,10 +12,10 @@ public class CookieProducer {
private static final String ACCESS_TOKEN_COOKIE_NAME = "access_token";
private static final String REFRESH_TOKEN_COOKIE_NAME = "refresh_token";

public List<Cookie> extractAuthCookies(TokenDto tokenDto) {
public List<Cookie> extractAuthCookies(AuthTokenDto authToken) {
List<Cookie> cookies = new ArrayList<>();
cookies.add(createCookie(ACCESS_TOKEN_COOKIE_NAME, tokenDto.accessToken()));
cookies.add(createCookie(REFRESH_TOKEN_COOKIE_NAME, tokenDto.refreshToken()));
cookies.add(createCookie(ACCESS_TOKEN_COOKIE_NAME, authToken.accessToken()));
cookies.add(createCookie(REFRESH_TOKEN_COOKIE_NAME, authToken.refreshToken()));
return cookies;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.zzang.chongdae.auth.service;

import com.zzang.chongdae.auth.exception.AuthErrorCode;
import com.zzang.chongdae.auth.service.dto.AuthInfoDto;
import com.zzang.chongdae.auth.service.dto.AuthMemberDto;
import com.zzang.chongdae.auth.service.dto.AuthTokenDto;
import com.zzang.chongdae.auth.service.dto.LoginRequest;
import com.zzang.chongdae.auth.service.dto.SignupRequest;
import com.zzang.chongdae.auth.service.dto.SignupResponseDto;
import com.zzang.chongdae.auth.service.dto.TokenDto;
import com.zzang.chongdae.global.exception.MarketException;
import com.zzang.chongdae.member.exception.MemberErrorCode;
import com.zzang.chongdae.member.repository.MemberRepository;
Expand All @@ -23,26 +24,31 @@ public class AuthService {
private final JwtTokenProvider jwtTokenProvider;
private final NicknameGenerator nickNameGenerator;

public TokenDto login(LoginRequest request) {
public AuthInfoDto login(LoginRequest request) {
String password = passwordEncoder.encode(request.ci());
MemberEntity member = memberRepository.findByPassword(password)
.orElseThrow(() -> new MarketException(AuthErrorCode.INVALID_PASSWORD));
return jwtTokenProvider.createAuthToken(member.getId().toString());
return createTokenByMember(member);
}

@Transactional
public SignupResponseDto signup(SignupRequest request) {
public AuthInfoDto signup(SignupRequest request) {
String password = passwordEncoder.encode(request.ci());
if (memberRepository.existsByPassword(password)) {
throw new MarketException(AuthErrorCode.DUPLICATED_MEMBER);
}
MemberEntity member = new MemberEntity(nickNameGenerator.generate(), password);
MemberEntity savedMember = memberRepository.save(member);
TokenDto tokenDto = jwtTokenProvider.createAuthToken(savedMember.getId().toString());
return new SignupResponseDto(savedMember, tokenDto);
return createTokenByMember(savedMember);
}

public TokenDto refresh(String refreshToken) {
private AuthInfoDto createTokenByMember(MemberEntity member) {
AuthMemberDto authMember = new AuthMemberDto(member);
AuthTokenDto authToken = jwtTokenProvider.createAuthToken(member.getId().toString());
return new AuthInfoDto(authMember, authToken);
}

public AuthTokenDto refresh(String refreshToken) {
Long memberId = jwtTokenProvider.getMemberIdByRefreshToken(refreshToken);
return jwtTokenProvider.createAuthToken(memberId.toString());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.zzang.chongdae.auth.service;

import com.zzang.chongdae.auth.exception.AuthErrorCode;
import com.zzang.chongdae.auth.service.dto.TokenDto;
import com.zzang.chongdae.auth.service.dto.AuthTokenDto;
import com.zzang.chongdae.global.exception.MarketException;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
Expand Down Expand Up @@ -36,8 +36,8 @@ public JwtTokenProvider(@Value("${security.jwt.token.access-secret-key}") String
this.clock = clock;
}

public TokenDto createAuthToken(String payload) {
return new TokenDto(createToken(payload, accessSecretKey, accessTokenExpired),
public AuthTokenDto createAuthToken(String payload) {
return new AuthTokenDto(createToken(payload, accessSecretKey, accessTokenExpired),
createToken(payload, refreshSecretKey, refreshTokenExpired));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.zzang.chongdae.auth.service.dto;

public record AuthInfoDto(AuthMemberDto authMember, AuthTokenDto authToken) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import com.zzang.chongdae.member.repository.entity.MemberEntity;

public record SignupMemberDto(Long id, String nickname) {
public record AuthMemberDto(Long id, String nickname) {

public SignupMemberDto(MemberEntity member) {
public AuthMemberDto(MemberEntity member) {
this(member.getId(), member.getNickname());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.zzang.chongdae.auth.service.dto;

public record AuthTokenDto(String accessToken, String refreshToken) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.zzang.chongdae.auth.service.dto;

public record LoginResponse(Long memberId, String nickname) {

public LoginResponse(AuthMemberDto authMember) {
this(authMember.id(), authMember.nickname());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public record SignupResponse(Long memberId, String nickname) {

public SignupResponse(SignupResponseDto output) {
this(output.member().id(), output.member().nickname());
public SignupResponse(AuthMemberDto authMember) {
this(authMember.id(), authMember.nickname());
}
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class Login {
List<FieldDescriptor> requestDescriptors = List.of(
fieldWithPath("ci").description("회원 식별자 인증 정보")
);
List<FieldDescriptor> responseDescriptors = List.of(
fieldWithPath("memberId").description("회원 id"),
fieldWithPath("nickname").description("닉네임")
);
List<HeaderDescriptorWithType> responseHeaderDescriptors = List.of(
headerWithName("Set-Cookie").description("""
access_token=a.b.c; Path=/; HttpOnly \n
Expand All @@ -46,8 +50,10 @@ class Login {
.summary("회원 로그인")
.description("회원 식별자 인증 정보로 로그인 합니다.")
.requestFields(requestDescriptors)
.responseFields(responseDescriptors)
.responseHeaders(responseHeaderDescriptors)
.requestSchema(schema("LonginRequest"))
.responseSchema(schema("LoginResponse"))
.build();

MemberEntity member;
Expand Down

0 comments on commit 0c3b932

Please sign in to comment.