Skip to content

Commit

Permalink
Feat: 로그인, 인증된 유저 조회 기능 완성 및 Account 메소드 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
vividswan committed Oct 22, 2021
1 parent c04e1ac commit c0cf3b0
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/main/java/com/server/wupitch/account/AccountController.java
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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@

public interface AccountRepository extends JpaRepository<Account, Long> {
Optional<Account> findByOAuthIdAndStatus(String oAuathId, Status valid);
Optional<Account> findByEmailAndStatus(String email, Status valid);
}
52 changes: 52 additions & 0 deletions src/main/java/com/server/wupitch/account/AccountService.java
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;
}
}
27 changes: 27 additions & 0 deletions src/main/java/com/server/wupitch/account/entity/Account.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
package com.server.wupitch.account.entity;

import com.server.wupitch.account.dto.AccountAuthDto;
import com.server.wupitch.account.entity.enumtypes.OAuthType;
import com.server.wupitch.area.Area;
import com.server.wupitch.configure.entity.BaseTimeEntity;
import com.server.wupitch.configure.entity.Status;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import com.server.wupitch.account.entity.enumtypes.RoleType;

import javax.persistence.*;

import static com.server.wupitch.account.entity.enumtypes.OAuthType.*;
import static com.server.wupitch.account.entity.enumtypes.RoleType.*;
import static com.server.wupitch.configure.entity.Status.*;

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Entity
Expand Down Expand Up @@ -45,4 +52,24 @@ public class Account extends BaseTimeEntity {

private String introduction;

public static Account createAccount(AccountAuthDto dto) {
return Account.builder()
.status(VALID)
.nickname(dto.getNickname())
.email(dto.getEmail())
.role(ROLE_USER)
.password(dto.getPassword())
.oAuth(KAKAO)
.oAuthId(dto.getOAuthId()).build();
}

public AccountAuthDto getAccountInfoDto() {
return AccountAuthDto.builder()
.accountId(this.accountId)
.email(this.email)
.nickname(this.nickname)
.oAuthId(this.oAuthId)
.build();
}

}

0 comments on commit c0cf3b0

Please sign in to comment.