Skip to content

Commit

Permalink
Merge pull request #9 from ssafy-19-final-pjt/feature/#1-oauth-login
Browse files Browse the repository at this point in the history
Feature/#1 oauth login
  • Loading branch information
gurwls0122 authored May 19, 2024
2 parents 20e7ef5 + 9e2a5df commit bc8f96d
Show file tree
Hide file tree
Showing 25 changed files with 393 additions and 91 deletions.
6 changes: 5 additions & 1 deletion backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ dependencies {
// openfeign
implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'

// 카카오 테스트를 위해 임시 추가
// 카카오 로그인, 메일 html 설정
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect'

// 이메일 전송 (비밀번호 찾기)
implementation 'org.springframework.boot:spring-boot-starter-mail'
}

dependencyManagement {
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,37 +1,53 @@
package com.ssafy.home.domain.member.controller;

import com.ssafy.home.domain.member.dto.request.FindPasswordRequest;
import com.ssafy.home.domain.member.dto.request.LoginRequest;
import com.ssafy.home.domain.member.dto.request.UpdatePasswordRequest;
import com.ssafy.home.domain.member.dto.request.RegisterRequest;
import com.ssafy.home.domain.member.dto.response.TokenResponse;
import com.ssafy.home.domain.member.repository.MemberRepository;
import com.ssafy.home.domain.member.service.MemberService;
import com.ssafy.home.global.auth.dto.MemberDto;
import com.ssafy.home.global.auth.jwt.JwtTokenProvider;
import com.ssafy.home.global.auth.validator.RefreshTokenValidator;
import com.ssafy.home.global.error.ErrorCode;
import com.ssafy.home.global.error.exception.AuthenticationException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@Tag(name = "member", description = "회원 API")
@RestController
@RequestMapping("/user")
@RequestMapping("/member")
@RequiredArgsConstructor
public class MemberController {

private final MemberService memberService;
private final RefreshTokenValidator refreshTokenValidator;
private final MemberRepository memberRepository;

@PostMapping("/")
@Tag(name = "authentication")
@Operation(summary = "일반 회원가입 API", description = "일반 회원가입 API")
@PostMapping(path = "/", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> register(@RequestBody RegisterRequest registerRequest){

memberService.register(registerRequest);

return ResponseEntity.status(HttpStatus.OK).body("signup success");
}

@Tag(name = "authentication")
@Operation(summary = "일반 로그인 API", description = "일반 로그인 API")
@PostMapping("/login")
public ResponseEntity<String> login(@RequestBody LoginRequest loginRequest, HttpServletResponse response){

Expand All @@ -48,6 +64,8 @@ public ResponseEntity<String> login(@RequestBody LoginRequest loginRequest, Http
return ResponseEntity.status(HttpStatus.OK).body("token success");
}

@Tag(name = "authentication")
@Operation(summary = "액세스 토큰 재발급 API", description = "액세스 토큰 재발급 API")
@GetMapping("/accessToken")
public ResponseEntity<String> createAccessToken(HttpServletRequest request, HttpServletResponse response){

Expand All @@ -63,8 +81,59 @@ public ResponseEntity<String> createAccessToken(HttpServletRequest request, Http
.orElse(null);
}

if(!refreshTokenValidator.validate(refreshToken)){
throw new AuthenticationException(ErrorCode.REFRESH_TOKEN_NOT_FOUND);
}

response.addHeader(JwtTokenProvider.AUTHORIZATION_HEADER, memberService.reissue(refreshToken));

return ResponseEntity.status(HttpStatus.OK).body("access token");
}

@Tag(name = "authentication")
@Operation(summary = "로그아웃 API", description = "로그아웃 API")
@GetMapping("/logout")
public ResponseEntity<String> logout(@AuthenticationPrincipal MemberDto memberDto,
HttpServletResponse response) {

Cookie myCookie = new Cookie("refreshToken", null);
myCookie.setMaxAge(0); // 쿠키의 expiration 타임을 0으로 하여 없앤다.
myCookie.setPath("/"); // 모든 경로에서 삭제 됬음을 알린다.
response.addCookie(myCookie);

memberService.removeRefreshToken(memberDto.getId());

return ResponseEntity.status(HttpStatus.OK).body("logout success");
}

@Tag(name = "authentication")
@Operation(summary = "비밀번호 변경 API", description = "비밀번호 변경 API")
@PostMapping("/pw")
public ResponseEntity<String> updatePassword(@AuthenticationPrincipal MemberDto memberDto,
@RequestBody UpdatePasswordRequest updatePasswordRequest){

memberService.updatePassword(memberDto.getId(),updatePasswordRequest.getPassword());

return ResponseEntity.status(HttpStatus.OK).body("updatePassword success");
}

@Tag(name = "authentication")
@Operation(summary = "비밀번호 찾기 API", description = "비밀번호 찾기 API")
@PostMapping("/findPw")
public ResponseEntity<String> findPassword(@RequestBody FindPasswordRequest findPasswordRequest){

memberService.sendPassword(findPasswordRequest.getEmail());

return ResponseEntity.status(HttpStatus.OK).body(findPasswordRequest.getEmail() + "send password success");
}

@Tag(name = "authentication")
@Operation(summary = "유저 정보 가져오기 API", description = "유저 정보 가져오기 API")
@PostMapping("/info")
public ResponseEntity<MemberDto> findPassword(@AuthenticationPrincipal MemberDto memberDto){

return ResponseEntity.status(HttpStatus.OK).body(memberDto);
}


}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.ssafy.home.domain.member.controller;

import com.ssafy.home.domain.member.dto.OauthLoginDto;
import com.ssafy.home.entity.member.MemberType;
import com.ssafy.home.domain.member.service.OauthLoginService;
import com.ssafy.home.entity.member.MemberType;
import com.ssafy.home.global.auth.validator.AuthorizationHeaderUtils;
import com.ssafy.home.global.auth.validator.OauthValidator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
Expand All @@ -26,7 +28,7 @@ public class OauthLoginController {

@Tag(name = "authentication")
@Operation(summary = "소셜 로그인 API", description = "소셜 로그인 API")
@PostMapping("/login")
@PostMapping(path = "/login", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<OauthLoginDto.Response> oauthLogin(@RequestBody OauthLoginDto.Request oauthLoginRequestDto,
HttpServletRequest httpServletRequest) {

Expand All @@ -38,9 +40,7 @@ public ResponseEntity<OauthLoginDto.Response> oauthLogin(@RequestBody OauthLogin
OauthLoginDto.Response jwtTokenResponseDto = oauthLoginService
.oauthLogin(accessToken, MemberType.from(oauthLoginRequestDto.getMemberType()));



return ResponseEntity.ok(jwtTokenResponseDto);
return ResponseEntity.status(HttpStatus.OK).body(jwtTokenResponseDto);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.ssafy.home.domain.member.dto.request;

import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class FindPasswordRequest {
private String email;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.ssafy.home.domain.member.dto.request;

import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class UpdatePasswordRequest {
private String password;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.ssafy.home.domain.member.dto.response;

import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@NoArgsConstructor
public class FindPasswordResponse {
private String receiveAddress;
private String mailTitle;
private String mailContent;

@Builder
public FindPasswordResponse(String receiveAddress, String mailTitle, String mailContent) {
this.receiveAddress = receiveAddress;
this.mailTitle = mailTitle;
this.mailContent = mailContent;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import com.ssafy.home.entity.member.Member;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Optional;

public interface MemberRepository extends JpaRepository<Member, Long> {
boolean existsByEmail(String email);

@Query("Select m from member m join fetch m.generalMember join fetch m.generalMember.memberSecret")
Optional<Member> findByEmail(String email);
@Query("Select m from member m join fetch m.generalMember join fetch m.generalMember.memberSecret where m.email = :email")
Optional<Member> findByEmail(@Param("email") String email);

}
Loading

0 comments on commit bc8f96d

Please sign in to comment.