Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/#14 Backend Member Nickname (회원가입 시 닉네임 입력) #34

Merged
merged 5 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.rollthedice.backend.domain.member.controller;

import com.rollthedice.backend.domain.member.dto.SignUpDto;
import com.rollthedice.backend.domain.member.service.MemberService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class LoginController {
private final MemberService memberService;

@ResponseStatus(HttpStatus.OK)
@PostMapping("/oauth2/sign-up")
public void signUp(@RequestBody SignUpDto dto) {
memberService.signUp(dto);
}

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

import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@Getter
public class SignUpDto {
private String nickname;
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,8 @@ public Member update(String email, String imageUrl) {
public void updateRefreshToken(String updateRefreshToken) {
this.refreshToken = updateRefreshToken;
}

public void updateNickname(String nickname) {
this.nickname = nickname;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.rollthedice.backend.domain.member.entity;

public enum Role {
ADMIN("ROLE_ADMIN"),
GUEST("ROLE_GUEST"),
USER("ROLE_USER");

private final String role;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.rollthedice.backend.domain.member.query;

import com.rollthedice.backend.domain.member.entity.Member;
import com.rollthedice.backend.domain.member.repository.MemberRepository;
import com.rollthedice.backend.global.jwt.service.JwtService;
import com.rollthedice.backend.global.query.QueryService;
import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;

@Slf4j
@QueryService
@RequiredArgsConstructor
public class AuthService {
private final MemberRepository memberRepository;
private final JwtService jwtService;

public Long getMemberId() {
return getMember().getId();
}

public Member getMember() {

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
UserDetails userDetails = (UserDetails) authentication.getPrincipal();
return memberRepository.findByEmail(userDetails.getUsername()).orElseThrow(EntityNotFoundException::new);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.rollthedice.backend.domain.member.service;

import com.rollthedice.backend.domain.member.dto.SignUpDto;
import com.rollthedice.backend.domain.member.entity.Member;
import com.rollthedice.backend.domain.member.query.AuthService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@RequiredArgsConstructor
@Service
public class MemberService {
private final AuthService authService;

@Transactional
public void signUp(SignUpDto dto) {
Member member = authService.getMember();
member.updateNickname(dto.getNickname());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public Member toEntity(SocialType socialType, OAuth2UserInfo oauth2UserInfo) {
.email(oauth2UserInfo.getEmail())
.nickname(oauth2UserInfo.getNickname())
.imageUrl(oauth2UserInfo.getImageUrl())
.role(Role.USER)
.role(Role.GUEST)
.status(Status.ACTIVE)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.rollthedice.backend.global.oauth2.handler;

import com.rollthedice.backend.domain.member.entity.Role;
import com.rollthedice.backend.global.jwt.service.JwtService;
import com.rollthedice.backend.global.oauth2.CustomOAuth2User;
import jakarta.servlet.ServletException;
Expand All @@ -21,10 +22,19 @@ public class OAuth2LoginSuccessHandler implements AuthenticationSuccessHandler {

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
log.info("OAuth2 Login 성공!");
CustomOAuth2User oAuth2User = (CustomOAuth2User) authentication.getPrincipal();
loginSuccess(response, oAuth2User); // 로그인에 성공한 경우 access, refresh 토큰 생성
log.info("OAuth2 Login succeed.");
CustomOAuth2User oAuth2User = (CustomOAuth2User) authentication.getPrincipal();

if(oAuth2User.getRole() == Role.GUEST) {
String accessToken = jwtService.createAccessToken(oAuth2User.getEmail());
response.addHeader(jwtService.getAccessHeader(), "Bearer " + accessToken);
response.sendRedirect("oauth2/sign-up");

jwtService.sendAccessAndRefreshToken(response, accessToken, null);
} else {
loginSuccess(response, oAuth2User);
response.sendRedirect("/");
}
}

private void loginSuccess(HttpServletResponse response, CustomOAuth2User oAuth2User) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.rollthedice.backend.global.query;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Service
@Transactional(readOnly = true)
public @interface QueryService {
}