Skip to content

Commit

Permalink
Feat: 회원비밀번호수정, 이메일수정, 회원삭제 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
s2hoon committed Jan 6, 2024
1 parent fb7cfcb commit 8ee3b4b
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 9 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ dependencies {
// school web mail
implementation 'com.github.in-seo:univcert:master-SNAPSHOT'

// password mail
implementation 'org.springframework.boot:spring-boot-starter-mail'


compileOnly 'org.projectlombok:lombok'
Expand Down
41 changes: 37 additions & 4 deletions src/main/java/com/umc/StudyFlexBE/controller/MemberController.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.umc.StudyFlexBE.controller;


import com.umc.StudyFlexBE.config.jwt.JwtTokenProvider;
import com.umc.StudyFlexBE.dto.request.CheckAuthCodeDto;
import com.umc.StudyFlexBE.dto.request.LoginDto;
import com.umc.StudyFlexBE.dto.request.SearchPasswordDto;
import com.umc.StudyFlexBE.dto.request.SendAuthCodeDto;
import com.umc.StudyFlexBE.dto.request.SignUpDto;
import com.umc.StudyFlexBE.dto.request.SignUpOAuthDto;
Expand All @@ -21,22 +21,21 @@
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("app/member")
@RequiredArgsConstructor
public class MemberController {

private final MemberService memberService;
private final RestTemplate restTemplate;
private final JwtTokenProvider jwtTokenProvider;

@Value("${mail.api.key}")
private String mail_api_key;

Expand Down Expand Up @@ -140,9 +139,43 @@ public BaseResponse<?> checkAuthCode(@RequestBody CheckAuthCodeDto checkAuthCode
return new BaseResponse<>(BaseResponseStatus.SUCCESS, "인증 코드 확인 완료.");
}

@DeleteMapping("/deleteMember")
public BaseResponse<?> deleteMember(String password) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String email = authentication.getName();
memberService.deleteMember(email);
return new BaseResponse<>(BaseResponseStatus.SUCCESS, "회원 삭제 완료.");
}


@GetMapping("/searchPassword")
public BaseResponse<?> searchPassword(@RequestBody SearchPasswordDto searchPasswordDto) {
try {
memberService.sendPasswordMail(searchPasswordDto.getEmail(), searchPasswordDto.getPassword());
return new BaseResponse<>(BaseResponseStatus.SUCCESS, "새로운 비밀번호 전송 완료.");
} catch (BaseException e) {
return new BaseResponse<>(BaseResponseStatus.CHANGE_PASSWORD_FAILED);
}
}

@PostMapping("/changeEmail")
public BaseResponse<?> changeEmail(String newEmail) {
try {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String email = authentication.getName();
memberService.changeEmail(email , newEmail);
return new BaseResponse<>(BaseResponseStatus.SUCCESS, "이메일 변경 완료.");
} catch (BaseException e) {
return new BaseResponse<>(BaseResponseStatus.CHANGE_EMAIL_FAILED);
}

}


@GetMapping("testauth")
@PreAuthorize("hasAnyRole('USER','ADMIN')")
public BaseResponse<?> test() {

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String email = authentication.getName();
System.out.println(email);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.umc.StudyFlexBE.dto.request;

import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@NoArgsConstructor
public class SearchPasswordDto {
@JsonProperty
@NotNull
private String email;

@JsonProperty
@NotNull
private String password;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public enum BaseResponseStatus {

SEND_EMAIL_FAILED(false,3005 ,"인증코드 요청 실패" ),
WEB_MAIL_CODE_FAILED(false, 3006, "인증코드 불일치"),
MAIL_SEND_FAILED(false, 3007,"이메일 전송 실패" ),
CHANGE_EMAIL_FAILED(false,3008 ,"이메일 변경 실패" ),
CHANGE_PASSWORD_FAILED(false,3009 ,"비밀번호 변경 실패" ),

/**
* 4XXX : Post
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@
public interface MemberRepository extends JpaRepository<Member, Long> {
Member findByEmail(String email);

Long deleteByEmail(String email);


}
62 changes: 58 additions & 4 deletions src/main/java/com/umc/StudyFlexBE/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@
import com.umc.StudyFlexBE.entity.Member;
import com.umc.StudyFlexBE.entity.OAuthProfile;
import com.umc.StudyFlexBE.repository.MemberRepository;
import jakarta.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.core.Authentication;
Expand All @@ -43,7 +46,7 @@ public class MemberService {
private final AuthenticationManagerBuilder authenticationManagerBuilder;
private final JwtTokenProvider jwtTokenProvider;
private final RestTemplate restTemplate;

private final JavaMailSender javaMailSender;


public boolean checkEmail(String email) {
Expand Down Expand Up @@ -107,8 +110,6 @@ public String login(LoginDto loginDto) {
}




public KaKaoOAuthToken getKakaoToken(String code) {
try {
HttpHeaders headers = new HttpHeaders();
Expand Down Expand Up @@ -173,4 +174,57 @@ public void certifyWebMail(String email, String school, String webEmail) {
memberRepository.save(member);
}

}
@Transactional
public void deleteMember(String email) {

Long memberId = memberRepository.deleteByEmail(email);

}


@Transactional
public void sendPasswordMail(String email, String password) {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage, "utf-8");

try {
// 1. 메일 수신자 설정
messageHelper.setTo(email);
// 2. 메일 제목 설정
messageHelper.setSubject("[StudyFlex] Your New Password");
// 3. 메일 내용 설정
String mailContent = "[StudyFlex] Your new password is: " + password;
messageHelper.setText(mailContent, true);
// 4. 메일 전송
javaMailSender.send(mimeMessage);
} catch (Exception e) {
throw new BaseException(BaseResponseStatus.MAIL_SEND_FAILED);
}

Member member = memberRepository.findByEmail(email);
if (member == null) {
throw new BaseException(BaseResponseStatus.NO_SUCH_EMAIL);
}
member.setPassword(passwordEncoder.encode(password));
memberRepository.save(member);


}


@Transactional
public void changeEmail(String email, String newEmail) {

Member member = memberRepository.findByEmail(email);
if (member == null) {
throw new BaseException(BaseResponseStatus.NO_SUCH_EMAIL);
}
if(checkEmail(newEmail) == false){
throw new BaseException(BaseResponseStatus.CHANGE_EMAIL_FAILED);
}

member.setEmail(newEmail);
memberRepository.save(member);

}
}
13 changes: 12 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ naver.redirect.url=http://localhost:8080/app/naver/callback



# mail api key
# univ mail api key
mail.api.key=97895d00-0dbe-4a3f-9d8e-bbe19e662d04




# Mail Setting
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=s4hoon
spring.mail.password=qsfc cdea ngeb nnno
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.auth=true

0 comments on commit 8ee3b4b

Please sign in to comment.