Skip to content

Commit

Permalink
Merge pull request #116 from BOOK-TALK/#115-delete-default-login
Browse files Browse the repository at this point in the history
#115 delete default login
  • Loading branch information
chanwoo7 authored Aug 25, 2024
2 parents f7a1ce7 + 26f415f commit 9c6b9ed
Show file tree
Hide file tree
Showing 19 changed files with 251 additions and 628 deletions.
Original file line number Diff line number Diff line change
@@ -1,65 +1,34 @@
package com.book.backend.domain.auth.controller;

import com.book.backend.domain.auth.dto.JwtTokenDto;
import com.book.backend.domain.auth.dto.LoginDto;
import com.book.backend.domain.auth.dto.LoginSuccessResponseDto;
import com.book.backend.domain.auth.dto.SignupDto;
import com.book.backend.domain.auth.service.AppleService;
import com.book.backend.domain.auth.service.AuthService;
import com.book.backend.domain.auth.service.KakaoService;
import com.book.backend.domain.user.dto.UserDto;
import com.book.backend.domain.auth.service.OAuthService;
import com.book.backend.domain.oidc.Provider;
import com.book.backend.global.ResponseTemplate;
import com.book.backend.global.log.RequestLogger;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/auth")
@RequiredArgsConstructor
@Slf4j
@Tag(name = "유저 관리", description = "회원 탈퇴 / 회원가입 / JWT 토큰 재발급 / 로그아웃 / 로그인 / 카카오 로그인 / 애플 로그인")
@Tag(name = "유저 관리", description = "회원 탈퇴 / JWT 토큰 재발급 / 로그아웃 / 카카오 로그인 / 애플 로그인")
public class AuthController {

private final AuthService authService;
private final KakaoService kakaoService;
private final AppleService appleService;
private final OAuthService OAuthService;
private final ResponseTemplate responseTemplate;

@Operation(summary = "회원가입", description = "기본 회원가입을 진행합니다.",
responses = {@ApiResponse(responseCode = "201", content = @Content(schema = @Schema(implementation = UserDto.class)),
description = UserDto.description)})
@PostMapping("/signup")
public ResponseEntity<?> signup(@Valid @RequestBody SignupDto signupDto) {
RequestLogger.body(signupDto);

UserDto userDto = authService.signup(signupDto);
return responseTemplate.success(userDto, HttpStatus.CREATED);
}

@Operation(summary = "로그인", description = "기본 로그인을 진행합니다.",
responses = {@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = LoginSuccessResponseDto.class)),
description = LoginSuccessResponseDto.description)})
@PostMapping("/login")
public ResponseEntity<?> login(@Valid @RequestBody LoginDto loginDto) {
RequestLogger.body(loginDto);

LoginSuccessResponseDto loginSuccessResponseDto = authService.login(loginDto);
return responseTemplate.success(loginSuccessResponseDto, HttpStatus.OK);
}

@Operation(summary = "로그아웃", description = "로그아웃을 진행합니다.",
responses = {@ApiResponse(responseCode = "200", description = "로그아웃이 완료되었습니다.")})
@PostMapping("/logout")
Expand All @@ -86,7 +55,7 @@ public ResponseEntity<?> deleteAccount(HttpServletRequest request) {
description = LoginSuccessResponseDto.description)})
@PostMapping("/kakaoLogin")
public ResponseEntity<?> kakaoLogin(@RequestParam String idToken) {
LoginSuccessResponseDto loginSuccessResponseDto = kakaoService.kakaoLogin(idToken);
LoginSuccessResponseDto loginSuccessResponseDto = OAuthService.oAuthLogin(Provider.KAKAO, idToken);

return responseTemplate.success(loginSuccessResponseDto, HttpStatus.OK);
}
Expand All @@ -99,7 +68,7 @@ public ResponseEntity<?> kakaoLogin(@RequestParam String idToken) {
description = LoginSuccessResponseDto.description)})
@PostMapping("/appleLogin")
public ResponseEntity<?> appleLogin(@RequestParam String idToken) {
LoginSuccessResponseDto loginSuccessResponseDto = appleService.appleLogin(idToken);
LoginSuccessResponseDto loginSuccessResponseDto = OAuthService.oAuthLogin(Provider.APPLE, idToken);

return responseTemplate.success(loginSuccessResponseDto, HttpStatus.OK);
}
Expand Down

This file was deleted.

This file was deleted.

17 changes: 0 additions & 17 deletions src/main/java/com/book/backend/domain/auth/dto/LoginDto.java

This file was deleted.

33 changes: 0 additions & 33 deletions src/main/java/com/book/backend/domain/auth/dto/SignupDto.java

This file was deleted.

27 changes: 0 additions & 27 deletions src/main/java/com/book/backend/domain/auth/mapper/AuthMapper.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
package com.book.backend.domain.auth.service;

import com.book.backend.domain.auth.dto.JwtTokenDto;
import com.book.backend.domain.auth.dto.LoginDto;
import com.book.backend.domain.auth.dto.LoginSuccessResponseDto;
import com.book.backend.domain.auth.dto.SignupDto;
import com.book.backend.domain.auth.mapper.AuthMapper;
import com.book.backend.domain.user.dto.UserDto;
import com.book.backend.domain.user.entity.User;
import com.book.backend.domain.user.mapper.UserMapper;
import com.book.backend.domain.user.repository.UserRepository;
import com.book.backend.domain.user.service.UserService;
import com.book.backend.exception.CustomException;
Expand All @@ -19,79 +13,24 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
@RequiredArgsConstructor
@Transactional(readOnly = true)
@Slf4j
public class AuthService {
private final UserRepository userRepository;
private final UserService userService;
private final AuthMapper authMapper;
private final UserMapper userMapper;
private final AuthenticationManager authenticationManager;
private final CustomUserDetailsService userDetailsService;
private final JwtUtil jwtUtil;
private final RedisTemplate<String, String> redisTemplate;

@Transactional
public UserDto signup(SignupDto signupDto) {
log.trace("AuthService > signup()");

userService.validateNotDuplicatedUsername(signupDto.getLoginId());
userService.validateNotDuplicatedNickname(signupDto.getNickname());

User user = authMapper.convertToUser(signupDto);
user.setRegDate(LocalDateTime.now());

User savedUser = userRepository.save(user);

return userMapper.convertToUserDto(savedUser);
}

@Transactional
public LoginSuccessResponseDto login(LoginDto loginDto) {
log.trace("AuthService > login()");

Authentication authentication;
try {
// 사용자 인증 시도
authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(loginDto.getLoginId(), loginDto.getPassword()));

// 인증 성공 시 Security Context에 인증 정보 저장
SecurityContextHolder.getContext().setAuthentication(authentication);
} catch (AuthenticationException e) {
throw new CustomException(ErrorCode.INVALID_CREDENTIALS);
}

// 인증 성공 후 유저 정보 로드
UserDetails userDetails = userDetailsService.loadUserByUsername(loginDto.getLoginId());
JwtTokenDto jwtTokenDto = jwtUtil.generateToken(userDetails);

User user = userRepository.findByLoginId(loginDto.getLoginId())
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND));

// Redis에 RefreshToken 저장
jwtUtil.storeRefreshTokenInRedis(authentication, jwtTokenDto.getRefreshToken());

return LoginSuccessResponseDto.builder()
.userId(user.getUserId())
.accessToken(jwtTokenDto.getAccessToken())
.refreshToken(jwtTokenDto.getRefreshToken())
.build();
}

public void logout(HttpServletRequest request) {
log.trace("AuthService > logout()");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@ public class CustomUserDetailsService implements UserDetailsService {

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
// username이 아닌 loginId로 간주
log.trace("CustomUserDetailsService > loadUserByUsername()");

User user = userRepository.findByLoginId(username)
.orElseGet(() -> userRepository.findByKakaoId(username)
.orElseGet(() -> userRepository.findByAppleId(username)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND))));
userRepository.findByKakaoId(username)
.orElseGet(() -> userRepository.findByAppleId(username)
.orElseThrow(() -> new CustomException(ErrorCode.USER_NOT_FOUND)));

return org.springframework.security.core.userdetails.User.builder()
.username(username)
.password(user.getPassword())
.password("unused")
.authorities("ROLE_USER")
.build();
}
Expand Down
Loading

0 comments on commit 9c6b9ed

Please sign in to comment.