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 #13 6차 세미나 과제 완료 #14

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

feat #13 6차 세미나 과제 완료 #14

wants to merge 2 commits into from

Conversation

minwoo0419
Copy link
Contributor

  • closes 6차 세미나 과제 #13

  • 회원가입 : member를 생성할 때 refreshToken과 accessToken이 함께 생성됩니다.

  • 로그인 : � memberId를 헤더로 받으면(아이디, 비밀번호 없이 임시로) 해당 id의 refreshToken과 accessToken을 생성합니다.

  • 토큰 재발급 : 헤더에 refreshToken을 넣어 보내면 accessToken을 재발급 해줍니다.

AuthController

@RestController
@RequestMapping("api/v1")
@RequiredArgsConstructor
public class AuthController {
    private final AuthService authService;
    @PostMapping("/auth/sign-up")
    public ResponseEntity<UserJoinResponse> signUp(
            @RequestBody MemberCreateDto memberCreate
    ) {
        UserJoinResponse userJoinResponse = authService.signUp(memberCreate);
        return ResponseEntity.status(HttpStatus.CREATED)
                .header("Location", userJoinResponse.refreshToken())
                .body(
                        userJoinResponse
                );
    }

    @PostMapping("/auth/sign-in")
    public ResponseEntity<UserJoinResponse> signIn(
            @RequestHeader Long memberId //아이디, 비밀번호 없으므로 임시 사용
    ) {
        UserJoinResponse userJoinResponse = authService.signIn(memberId);
        return ResponseEntity.status(HttpStatus.OK)
                .body(
                        userJoinResponse
                );
    }

    @PostMapping("/auth/re-issue")
    public ResponseEntity<UserJoinResponse> reIssue(HttpServletRequest request) {
        return ResponseEntity.ok(authService.reIssue(request));
    }
}

�Service 로직

  • memberService와 TokenService를 이어주는 AuthService를 생성하여 구현했습니다.
@Service
@RequiredArgsConstructor
public class AuthService {
    private final MemberService memberService;
    private final TokenService tokenService;
    @Transactional
    public UserJoinResponse signUp(
            final MemberCreateDto memberCreate
    ) {
        Long memberId = memberService.save(
                Member.create(memberCreate.name(), memberCreate.part(), memberCreate.age())
        );
        return tokenService.createTokens(memberId);
    }
    @Transactional
    public UserJoinResponse signIn(
            final Long memberId
    ) {
        return tokenService.createTokens(memberId);

    }
    public UserJoinResponse reIssue(final HttpServletRequest request){
        return tokenService.createAccessToken(request);
    }
}
  • refreshToken 저장 : memberId와 refreshToken값을 저장해주었습니다.
@Transactional
public Token save(
            final Long memberId,
            final String refreshToken
    ){
        Token token = Token.create(memberId, refreshToken);
        tokenRepository.save(token);
        return token;
}
  • refreshToken 과 accessToken 생성
@Transactional
public UserJoinResponse createTokens(final Long memberId){
        String accessToken = jwtTokenProvider.issueAccessToken(
                UserAuthentication.createUserAuthentication(memberId)
        );
        String refreshToken = jwtTokenProvider.issueRefreshToken(
                UserAuthentication.createUserAuthentication(memberId)
        );
        Token token = save(memberId, refreshToken);
        return UserJoinResponse.of(accessToken, token.getRefreshToken());
}
  • refreshToken을 이용해 accessToken 재발급
public UserJoinResponse createAccessToken(final HttpServletRequest request){
        Token token = findByRefreshToken(jwtTokenProvider.getJwtFromRequest(request));
        Long memberId = token.getId();
        String accessToken = jwtTokenProvider.issueAccessToken(
                UserAuthentication.createUserAuthentication(memberId)
        );
        return UserJoinResponse.of(accessToken, token.getRefreshToken());
}

API 명세서

Post 생성

Method URL
POST /api/v1/auth/sign-up

Path Parameter

query Parameter

Request Header

Request Body

name String
part String
age Integer

Response Body

status 201
accessToken String
refreshToken String

로그인

Method URL
POST /api/v1/auth/sign-in

Path Parameter

query Parameter

Request Header

memberId Long 멤버 id

Request Body

Response Body

status 200
accessToken String
refreshToken String

액세스 토큰 재발급

Method URL
POST /api/v1/auth/re-issue

Path Parameter

query Parameter

Request Header

Authorization String Bearer + refresh토큰

Request Body

Response Body

status 200
accessToken String
refreshToken String

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6차 세미나 과제
1 participant