-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
92 additions
and
41 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
seminar/week3/practice/src/main/java/org/sopt/practice/controller/AuthController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
seminar/week3/practice/src/main/java/org/sopt/practice/service/AuthService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters