Skip to content

Commit

Permalink
Feat: user 로직 수정 #1
Browse files Browse the repository at this point in the history
  • Loading branch information
tokyj515 committed Jul 12, 2023
1 parent 3a8af51 commit 6a1eec0
Show file tree
Hide file tree
Showing 8 changed files with 92 additions and 17 deletions.
13 changes: 13 additions & 0 deletions src/main/java/com/example/neoul/controller/TestController.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.example.neoul.controller;

import com.example.neoul.global.entity.ApiResponse;
import com.example.neoul.global.exception.BadRequestException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
Expand All @@ -16,4 +19,14 @@ public ResponseEntity test(){
log.info("test api 수신");
return new ResponseEntity("test ok",HttpStatus.OK);
}


@GetMapping("/")
public ApiResponse<String> test2(@RequestParam String s){

if("null".equals(s))
throw new BadRequestException("오류 던지기");

return new ApiResponse<>("데이터가 들어갈 자리(문자열, 객체 등");
}
}
64 changes: 64 additions & 0 deletions src/main/java/com/example/neoul/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.example.neoul.controller;


import com.example.neoul.service.UserService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;


@RequiredArgsConstructor
@RestController
@RequestMapping("/user")
public class UserController {

private final UserService userService;


// @ApiOperation(value = "현재 로그인 유저", notes = "현재 로그인 유저")
// @GetMapping("/now")
// public BaseResponse<User> now(){
//
// if(userService.findNowLoginUser() != null){
// return BaseResponse.ok(SUCCESS, userService.findNowLoginUser());
// }
//
// return BaseResponse.ok(USER_NEED_TO_LOGIN);
// }


// @ApiOperation(value = "회원가입", notes = "회원가입")
// @PostMapping("/signup")
// public BaseResponse<User> signup(@RequestBody UserReq.SignupUserReq signupUserReq) {
//
// if(!signupUserReq.isEmailVerified())
// return BaseResponse.ok(USER_EMAIL_VERIFIED_FALSE);
//
// return BaseResponse.ok(SUCCESS, userService.signup(signupUserReq));
// }
//
//
//
//
// @ApiOperation(value = "로그인", notes = "로그인")
// @PostMapping("/login")
// public BaseResponse<TokenRes> signup(@RequestBody UserReq.LoginUserReq loginUserReq) {
//
// return BaseResponse.ok(SUCCESS, userService.login(loginUserReq));
// }
//
//
// @ApiOperation(value = "회원 탈퇴", notes = "회원 탈퇴")
// @GetMapping("/withdrawal")
// public BaseResponse<String> withdrawal() {
// if(userService.withdrawal() != null)
// return BaseResponse.ok(SUCCESS, userService.withdrawal());
//
// return BaseResponse.ok(USER_FAILED_TO_WITHDRAWAL);
// }



}
11 changes: 6 additions & 5 deletions src/main/java/com/example/neoul/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,29 @@ public class User extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "user_id")
private Long userId;

private String username;
private String username; //로그인할 때 사용하는 아이디(이메일)

private String password;

private String name;

private String phone;

@Column(name = "image_url")
private String imageUrl;

private String social;

private Integer status;


@JsonIgnore
@ManyToMany
@JoinTable(
name = "user_authority",
joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "user_id")},
inverseJoinColumns = {@JoinColumn(name = "authority_name", referencedColumnName = "authority_name")})
private List<Authority> authorities;

Expand All @@ -65,7 +67,6 @@ public static User toSocialLoginUser(String email, String social, String name) {
.imageUrl("이미지url")
.authorities(Collections.singletonList(authority))
.social(social)
.status(1)
.build();

return user;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public Authentication getAuthentication(String token) {

Long userIdx = claims.get("userIdx",Long.class);

User user = userRepository.findUserById(userIdx).get();
User user = userRepository.findUserByUserId(userIdx).get();
String userName = user.getUsername();

UserDetails userDetails = customUserDetailsService.loadUserByUsername(userName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findUserByUsername(String email);


Optional<User> findUserById(Long userId);
Optional<User> findUserByUserId(Long userId);


User findByUsernameAndSocial(String username, String social);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/example/neoul/service/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public TokenRes createAndLoginKakaoUser(UserReq.SocialReq socialReq) {

//로그인
User user=userRepository.findByUsernameAndSocial(String.valueOf(email),socialReq.getSocial());
Long userIdx=user.getId();
Long userIdx=user.getUserId();

GenerateToken generateToken=tokenProvider.createAllToken(userIdx);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public UserDetails loadUserByUsername(final String username) {
}

private org.springframework.security.core.userdetails.User createUser(String username, User user) {
if (!user.getStatus().equals(1)) {
//throw new RuntimeException(username + " -> 활성화되어 있지 않습니다.");
throw new RuntimeException(username + " -> 탈퇴하거나 가입되지 않은 회원입니다");
}
// if (!user.getStatus().equals(1)) {
// //throw new RuntimeException(username + " -> 활성화되어 있지 않습니다.");
// throw new RuntimeException(username + " -> 탈퇴하거나 가입되지 않은 회원입니다");
// }
List<GrantedAuthority> grantedAuthorities = user.getAuthorities().stream()
.map(authority -> new SimpleGrantedAuthority(authority.getAuthorityName()))
.collect(Collectors.toList());
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/com/example/neoul/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ public User signup(UserReq.SignupUserReq signupUserReq) {
.name(signupUserReq.getName())
.imageUrl(signupUserReq.getImageUrl())
.authorities(Collections.singletonList(authority))
.status(1)
.build();

userRepository.save(user);
Expand Down Expand Up @@ -127,7 +126,6 @@ public User signupADMIN(UserReq.SignupUserReq signupUserReq) {
.imageUrl(signupUserReq.getImageUrl())
//.authorities(Collections.singletonList(authority))
.authorities(authorityList)
.status(1)
.build();

userRepository.save(user);
Expand All @@ -154,7 +152,7 @@ public TokenRes login(UserReq.LoginUserReq loginUserReq){
//String jwt = tokenProvider.createToken(authentication); //인증토큰으로 jwt토큰 생성

User loginUser = userRepository.findUserByUsername(loginUserReq.getUsername()).get();
Long userIdx = loginUser.getId();
Long userIdx = loginUser.getUserId();
GenerateToken generateToken = tokenProvider.createAllToken(userIdx);


Expand All @@ -168,8 +166,7 @@ public TokenRes login(UserReq.LoginUserReq loginUserReq){

public String withdrawal() {
User user = findNowLoginUser();

user.setStatus(0);

userRepository.save(user);

return "회원 탈퇴가 완료되었습니다";
Expand Down

0 comments on commit 6a1eec0

Please sign in to comment.