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

소셜로그인 구현 #56

Merged
merged 2 commits into from
Aug 4, 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
13 changes: 13 additions & 0 deletions src/main/java/kimandhong/oxox/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import kimandhong.oxox.domain.User;
import kimandhong.oxox.dto.user.JoinDto;
import kimandhong.oxox.dto.user.LoginDto;
import kimandhong.oxox.dto.user.SocialLoginDto;
import kimandhong.oxox.dto.user.UserDto;
import kimandhong.oxox.service.UserService;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -46,4 +47,16 @@ public ResponseEntity<UserDto> login(@RequestBody @Valid final LoginDto loginDto
.header("Access-Control-Expose-Headers", "X-Access-Token")
.body(UserDto.from(user));
}

@PostMapping("login/social")
@SwaggerCreated(summary = "소셜로그인")
public ResponseEntity<UserDto> socialLogin(@RequestBody @Valid final SocialLoginDto loginDto) {
final User user = userService.socialLogin(loginDto);
final String token = jwtUtil.createAccessToken(user);

return ResponseEntity.status(HttpStatus.OK)
.header("X-Access-Token", token)
.header("Access-Control-Expose-Headers", "X-Access-Token")
.body(UserDto.from(user));
}
}
10 changes: 10 additions & 0 deletions src/main/java/kimandhong/oxox/domain/Profile.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import kimandhong.oxox.dto.user.JoinDto;
import kimandhong.oxox.dto.user.SocialLoginDto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand Down Expand Up @@ -40,6 +41,15 @@ public static Profile from(final JoinDto joinDto, final User user, final Long se
.build();
}

public static Profile from(final SocialLoginDto loginDto, final Long sequence, final User user) {
return Profile.builder()
.nickname(loginDto.displayName())
.sequence(sequence)
.image(loginDto.photoUrl())
.user(user)
.build();
}

public void updateProfile(final String nickname, final Long sequence, final String image) {
this.image = image;
this.nickname = nickname;
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/kimandhong/oxox/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import kimandhong.oxox.dto.user.JoinDto;
import kimandhong.oxox.dto.user.SocialLoginDto;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -24,7 +25,6 @@ public class User extends TimeEntity {
@Column(nullable = false)
private String email;

@Column(nullable = false)
private String password;
private String uid;

Expand All @@ -50,7 +50,17 @@ private User(final JoinDto joinDto, final String password, final Long sequence,
this.profile = Profile.from(joinDto, this, sequence, profileImage);
}

private User(final SocialLoginDto loginDto, final Long sequence) {
this.email = loginDto.email();
this.uid = loginDto.uid();
this.profile = Profile.from(loginDto, sequence, this);
}

public static User from(final JoinDto joinDto, final String password, final Long sequence, final String profileImage) {
return new User(joinDto, password, sequence, profileImage);
}

public static User from(final SocialLoginDto loginDto, final Long sequence) {
return new User(loginDto, sequence);
}
}
9 changes: 9 additions & 0 deletions src/main/java/kimandhong/oxox/dto/user/SocialLoginDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package kimandhong.oxox.dto.user;

public record SocialLoginDto(
String email,
String displayName,
String photoUrl,
String uid
) {
}
2 changes: 2 additions & 0 deletions src/main/java/kimandhong/oxox/repository/UserRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public interface UserRepository extends JpaRepository<User, Long> {

@EntityGraph(attributePaths = "profile")
List<User> findAll();

Optional<User> findByUid(final String uid);
}
9 changes: 9 additions & 0 deletions src/main/java/kimandhong/oxox/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import kimandhong.oxox.domain.User;
import kimandhong.oxox.dto.user.JoinDto;
import kimandhong.oxox.dto.user.LoginDto;
import kimandhong.oxox.dto.user.SocialLoginDto;
import kimandhong.oxox.handler.error.ErrorCode;
import kimandhong.oxox.handler.error.exception.ConflictException;
import kimandhong.oxox.handler.error.exception.NotFoundException;
Expand Down Expand Up @@ -51,4 +52,12 @@ public User login(final LoginDto loginDto) {
.filter(foundUser -> passwordEncoder.matches(loginDto.password(), foundUser.getPassword()))
.orElseThrow(() -> new NotFoundException(ErrorCode.BAD_REQUEST_LOGIN));
}

public User socialLogin(final SocialLoginDto loginDto) {
return userRepository.findByUid(loginDto.uid()).orElseGet(() -> {
final Long sequence = profileCustomRepository.findMaxSequenceByNickname(loginDto.displayName()) + 1;
final User user = User.from(loginDto, sequence);
return userRepository.save(user);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE users
MODIFY COLUMN password VARCHAR(255) NULL;