Skip to content

Commit

Permalink
[FEAT] #11 auth APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
nykoh2001 committed May 31, 2024
1 parent 884a793 commit b960c65
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.sopt.practice.controller;

import lombok.RequiredArgsConstructor;
import org.sopt.practice.service.AuthService;
import org.sopt.practice.service.dto.MemberCreateDto;
import org.sopt.practice.service.dto.UserJoinResponse;
import org.springframework.http.HttpStatus;
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.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/auth")
public class AuthController {

private final AuthService authService;

@PostMapping
public ResponseEntity<UserJoinResponse> signUp(
@RequestBody MemberCreateDto memberCreate
) {
UserJoinResponse userJoinResponse = authService.signUp(memberCreate);
return ResponseEntity.status(HttpStatus.CREATED)
.header("Location", userJoinResponse.userId())
.body(
userJoinResponse
);
}

@PostMapping("/reissue")
public ResponseEntity<UserJoinResponse> signIn(
@RequestHeader("X-Refresh-Token") final String refreshToken
) {
UserJoinResponse userJoinResponse = authService.signIn(refreshToken);
return ResponseEntity.ok(userJoinResponse);
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
package org.sopt.practice.controller;

import java.net.URI;
import lombok.RequiredArgsConstructor;
import org.sopt.practice.service.MemberService;
import org.sopt.practice.service.dto.MemberCreateDto;
import org.sopt.practice.service.dto.MemberFindDto;
import org.sopt.practice.service.dto.UserJoinResponse;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

Expand All @@ -23,18 +17,6 @@ public class MemberController {

private final MemberService memberService;

@PostMapping
public ResponseEntity<UserJoinResponse> postMember(
@RequestBody MemberCreateDto memberCreate
) {
UserJoinResponse userJoinResponse = memberService.createMember(memberCreate);
return ResponseEntity.status(HttpStatus.CREATED)
.header("Location", userJoinResponse.userId())
.body(
userJoinResponse
);
}

@GetMapping("/{memberId}")
public ResponseEntity<MemberFindDto> findMemberById(
@PathVariable final Long memberId
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package org.sopt.practice.service;

import lombok.RequiredArgsConstructor;
import org.sopt.practice.auth.UserAuthentication;
import org.sopt.practice.auth.redis.domain.Token;
import org.sopt.practice.auth.redis.repository.RedisTokenRepository;
import org.sopt.practice.common.jwt.JwtTokenProvider;
import org.sopt.practice.domain.Member;
import org.sopt.practice.repository.MemberRepository;
import org.sopt.practice.service.dto.MemberCreateDto;
import org.sopt.practice.service.dto.UserJoinResponse;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class AuthService {

private final MemberRepository memberRepository;
private final JwtTokenProvider jwtTokenProvider;
private final RedisTokenRepository redisTokenRepository;

@Transactional
public UserJoinResponse signUp(
MemberCreateDto memberCreate
) {
Member member = memberRepository.save(
Member.create(memberCreate.name(), memberCreate.part(), memberCreate.age())
);
Long memberId = member.getId();
String accessToken = jwtTokenProvider.issueAccessToken(
UserAuthentication.createUserAuthentication(memberId)
);
String refreshToken = jwtTokenProvider.issueRefreshToken();
redisTokenRepository.save(Token.of(memberId, refreshToken));

return UserJoinResponse.of(accessToken, refreshToken, memberId.toString());
}

@Transactional
public UserJoinResponse signIn(
String refreshToken
) {
Token redisToken = redisTokenRepository.findByRefreshTokenOrElseThrow(refreshToken);
String newAccessToken = jwtTokenProvider.issueAccessToken(
UserAuthentication.createUserAuthentication(redisToken.getId())
);

return UserJoinResponse.of(newAccessToken, refreshToken, redisToken.getId().toString());
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package org.sopt.practice.service;

import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import org.sopt.practice.auth.UserAuthentication;
import org.sopt.practice.common.ErrorMessage;
import org.sopt.practice.common.jwt.JwtTokenProvider;
import org.sopt.practice.domain.Member;
import org.sopt.practice.exception.NotFoundException;
import org.sopt.practice.repository.MemberRepository;
import org.sopt.practice.service.dto.MemberCreateDto;
import org.sopt.practice.service.dto.MemberFindDto;
import org.sopt.practice.service.dto.UserJoinResponse;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -19,24 +14,6 @@
public class MemberService {

private final MemberRepository memberRepository;
private final JwtTokenProvider jwtTokenProvider;


@Transactional
public UserJoinResponse createMember(
MemberCreateDto memberCreate
) {
Member member = memberRepository.save(
Member.create(memberCreate.name(), memberCreate.part(), memberCreate.age())
);
Long memberId = member.getId();
String accessToken = jwtTokenProvider.issueAccessToken(
UserAuthentication.createUserAuthentication(memberId)
);
String refreshToken = jwtTokenProvider.issueRefreshToken();

return UserJoinResponse.of(accessToken, refreshToken, memberId.toString());
}

/* private -> protected로 다른 서비스 레이어에서 호출할 수 있도록 수정 */
protected Member findMemberById(final Long memberId) {
Expand Down

0 comments on commit b960c65

Please sign in to comment.