-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: 로그인, 인증된 유저 조회 기능 완성 및 Account 메소드 추가
- Loading branch information
Showing
4 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
src/main/java/com/server/wupitch/account/AccountController.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,37 @@ | ||
package com.server.wupitch.account; | ||
|
||
import com.server.wupitch.account.dto.AccountAuthDto; | ||
import com.server.wupitch.account.dto.SignInReq; | ||
import com.server.wupitch.account.dto.SignInRes; | ||
import com.server.wupitch.configure.response.DataResponse; | ||
import com.server.wupitch.configure.response.ResponseService; | ||
import com.server.wupitch.configure.security.authentication.CustomUserDetails; | ||
import com.server.wupitch.util.ValidationExceptionProvider; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.annotation.AuthenticationPrincipal; | ||
import org.springframework.validation.Errors; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
import javax.validation.Valid; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping(value = "/app") | ||
public class AccountController { | ||
|
||
private final AccountService accountService; | ||
private final ResponseService responseService; | ||
|
||
|
||
@PostMapping(value = "/sign-in") | ||
public DataResponse<SignInRes> signIn(@RequestBody @Valid SignInReq req, Errors errors) { | ||
if (errors.hasErrors()) ValidationExceptionProvider.throwValidError(errors); | ||
return responseService.getDataResponse(accountService.signIn(req)); | ||
} | ||
|
||
@GetMapping(value = "/accounts/auth") | ||
public DataResponse<AccountAuthDto> getAuthAccount(@AuthenticationPrincipal CustomUserDetails customUserDetails) { | ||
return responseService.getDataResponse(accountService.getAuthAccount(customUserDetails)); | ||
} | ||
|
||
} |
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
52 changes: 52 additions & 0 deletions
52
src/main/java/com/server/wupitch/account/AccountService.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,52 @@ | ||
package com.server.wupitch.account; | ||
|
||
import com.server.wupitch.account.dto.AccountAuthDto; | ||
import com.server.wupitch.account.dto.SignInReq; | ||
import com.server.wupitch.account.dto.SignInRes; | ||
import com.server.wupitch.account.entity.Account; | ||
import com.server.wupitch.configure.entity.Status; | ||
import com.server.wupitch.configure.response.exception.CustomException; | ||
import com.server.wupitch.configure.response.exception.CustomExceptionStatus; | ||
import com.server.wupitch.configure.security.authentication.CustomUserDetails; | ||
import com.server.wupitch.configure.security.jwt.JwtTokenProvider; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.crypto.password.PasswordEncoder; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import static com.server.wupitch.configure.entity.Status.VALID; | ||
|
||
|
||
@RequiredArgsConstructor | ||
@Transactional(readOnly = true) | ||
@Service | ||
public class AccountService { | ||
|
||
private final AccountRepository accountRepository; | ||
private final PasswordEncoder passwordEncoder; | ||
private final JwtTokenProvider jwtTokenProvider; | ||
|
||
|
||
@Transactional | ||
public SignInRes signIn(SignInReq req) { | ||
Account account = accountRepository.findByEmailAndStatus(req.getEmail(), VALID) | ||
.orElseThrow(()-> new CustomException(CustomExceptionStatus.FAILED_TO_LOGIN)); | ||
if(!passwordEncoder.matches(req.getPassword(),account.getPassword())){ | ||
throw new CustomException(CustomExceptionStatus.FAILED_TO_LOGIN); | ||
} | ||
|
||
SignInRes res = SignInRes.builder() | ||
.accountId(account.getAccountId()) | ||
.jwt(jwtTokenProvider.createToken(account.getEmail(), account.getRole())) | ||
.build(); | ||
|
||
return res; | ||
} | ||
|
||
public AccountAuthDto getAuthAccount(CustomUserDetails customUserDetails) { | ||
Account account = customUserDetails.getAccount(); | ||
AccountAuthDto accountInfoDto = account.getAccountInfoDto(); | ||
accountInfoDto.setJwt(jwtTokenProvider.createToken(account.getEmail(), account.getRole())); | ||
return accountInfoDto; | ||
} | ||
} |
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