-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#5] fix: add AuthController and refreshToken api
- userController에서 authController 분리 - jwtToken -> accessToken으로 명칭 변경 - /refresh api 추가
- Loading branch information
Showing
14 changed files
with
234 additions
and
211 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
spring/notinote/src/main/java/com/answer/notinote/Auth/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,54 @@ | ||
package com.answer.notinote.Auth.controller; | ||
|
||
import com.answer.notinote.Auth.service.AuthService; | ||
import com.answer.notinote.User.dto.JoinRequestDto; | ||
import com.answer.notinote.User.service.UserService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
public class AuthController { | ||
|
||
private final AuthService authService; | ||
|
||
/** | ||
* oauth2 로그인을 진행합니다. | ||
* @param token | ||
* @return | ||
*/ | ||
@GetMapping("/login/oauth2") | ||
public ResponseEntity<?> oauthLogin(HttpServletResponse response, @RequestHeader("Authorization") String token) { | ||
return ResponseEntity.ok(authService.oauthLogin(response, token)); | ||
} | ||
|
||
/** | ||
* 회원가입 폼 정보를 받아 유저의 권한을 USER로 바꾸고 로그인을 진행합니다. | ||
* @param requestDto | ||
* @return | ||
*/ | ||
@PostMapping("/join") | ||
public ResponseEntity<?> join(HttpServletResponse response, @RequestBody JoinRequestDto requestDto) { | ||
return ResponseEntity.ok(authService.join(response, requestDto)); | ||
} | ||
|
||
@PostMapping("/refresh") | ||
public ResponseEntity<?> refresh(HttpServletRequest request, HttpServletResponse response) { | ||
|
||
return ResponseEntity.ok(authService.refreshToken(request, response)); | ||
} | ||
|
||
/** | ||
* 회원을 로그아웃합니다. | ||
* @param request | ||
* @return | ||
*/ | ||
@DeleteMapping("/logout") | ||
public ResponseEntity<?> logout(HttpServletRequest request) { | ||
return ResponseEntity.ok(authService.logout(request)); | ||
} | ||
} |
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
125 changes: 125 additions & 0 deletions
125
spring/notinote/src/main/java/com/answer/notinote/Auth/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,125 @@ | ||
package com.answer.notinote.Auth.service; | ||
|
||
import com.answer.notinote.Auth.data.ProviderType; | ||
import com.answer.notinote.Auth.data.RoleType; | ||
import com.answer.notinote.Auth.repository.RefreshTokenRepository; | ||
import com.answer.notinote.Auth.token.RefreshToken; | ||
import com.answer.notinote.Auth.token.provider.JwtTokenProvider; | ||
import com.answer.notinote.Auth.userdetails.GoogleUser; | ||
import com.answer.notinote.Child.service.ChildService; | ||
import com.answer.notinote.Exception.CustomException; | ||
import com.answer.notinote.Exception.ErrorCode; | ||
import com.answer.notinote.User.domain.entity.User; | ||
import com.answer.notinote.User.domain.repository.UserRepository; | ||
import com.answer.notinote.User.dto.JoinRequestDto; | ||
import com.answer.notinote.User.dto.UserResponseDto; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import javax.servlet.http.HttpServletResponse; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class AuthService { | ||
private final OAuth2Service oAuthService; | ||
private final ChildService childService; | ||
private final UserRepository userRepository; | ||
private final RefreshTokenRepository refreshTokenRepository; | ||
private final JwtTokenProvider jwtTokenProvider; | ||
|
||
public UserResponseDto oauthLogin(HttpServletResponse response, String token) { | ||
ResponseEntity<String> userInfoResponse = oAuthService.createGetRequest(token); | ||
GoogleUser googleUser = oAuthService.getUserInfo(userInfoResponse); | ||
|
||
User user = userRepository.findByUemail(googleUser.getEmail()).orElse(null); | ||
if (user == null) { | ||
user = User.builder() | ||
.uemail(googleUser.getEmail()) | ||
.username(googleUser.getName()) | ||
.uroleType(RoleType.GUEST) | ||
.uproviderType(ProviderType.GOOGLE) | ||
.build(); | ||
userRepository.save(user); | ||
} | ||
else { | ||
issueToken(response, user); | ||
} | ||
|
||
return new UserResponseDto(user); | ||
} | ||
|
||
@Transactional | ||
public UserResponseDto join(HttpServletResponse response, JoinRequestDto requestDto) { | ||
User user = userRepository.findById(requestDto.getUid()).orElseThrow( | ||
() -> new CustomException(ErrorCode.USER_NOT_FOUND) | ||
); | ||
|
||
requestDto.getUchildren().forEach( childDto -> childService.create(childDto, user)); | ||
|
||
if (user.getUroleType() == RoleType.GUEST) { | ||
user.setUsername(requestDto.getUsername()); | ||
user.setUlanguage(requestDto.getUlanguage()); | ||
user.setUroleType(RoleType.USER); | ||
user.setUprofileImg(requestDto.getUprofileImg()); | ||
userRepository.save(user); | ||
|
||
issueToken(response, user); | ||
return new UserResponseDto(user); | ||
} | ||
else { | ||
throw new CustomException(ErrorCode.USER_DUPLICATED); | ||
} | ||
} | ||
|
||
private void issueToken(HttpServletResponse response, User user) { | ||
String accessToken = jwtTokenProvider.createToken(user); | ||
String refreshToken = jwtTokenProvider.createRefreshToken(user); | ||
|
||
jwtTokenProvider.setAccessToken(response, accessToken); | ||
jwtTokenProvider.setRefreshToken(response, refreshToken); | ||
} | ||
|
||
@Transactional | ||
public String refreshToken(HttpServletRequest request, HttpServletResponse response) { | ||
String accessToken = jwtTokenProvider.resolveAccessToken(request); | ||
String refreshToken = jwtTokenProvider.resolveRefreshToken(request); | ||
|
||
boolean validateRefreshToken = jwtTokenProvider.validateToken(refreshToken) && jwtTokenProvider.existsRefreshToken(refreshToken); | ||
if (jwtTokenProvider.validateTokenExpired(accessToken) && validateRefreshToken) { | ||
String email = jwtTokenProvider.getUserEmail(refreshToken); | ||
User user = findUserByEmail(email); | ||
|
||
String newAccessToken = jwtTokenProvider.createToken(user); | ||
jwtTokenProvider.setAccessToken(response, newAccessToken); | ||
|
||
return "ok"; | ||
} else { | ||
throw new CustomException(ErrorCode.TOKEN_NOT_EXPIRED); | ||
} | ||
} | ||
|
||
@Transactional | ||
public Long logout(HttpServletRequest request) { | ||
String token = jwtTokenProvider.resolveAccessToken(request); | ||
String email = jwtTokenProvider.getUserEmail(token); | ||
|
||
User user = findUserByEmail(email); | ||
|
||
RefreshToken refreshToken = refreshTokenRepository.findByUser(user).orElseThrow( | ||
() -> new CustomException(ErrorCode.TOKEN_NOT_FOUND) | ||
); | ||
refreshTokenRepository.delete(refreshToken); | ||
|
||
return user.getUid(); | ||
} | ||
|
||
private User findUserByEmail(String email) { | ||
return userRepository.findByUemail(email).orElseThrow( | ||
() -> new CustomException(ErrorCode.USER_NOT_FOUND) | ||
); | ||
} | ||
|
||
} |
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
26 changes: 0 additions & 26 deletions
26
spring/notinote/src/main/java/com/answer/notinote/Auth/service/RefreshTokenService.java
This file was deleted.
Oops, something went wrong.
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
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
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
Oops, something went wrong.