Skip to content

Commit

Permalink
hotfix: 선생님 로그인
Browse files Browse the repository at this point in the history
  • Loading branch information
ori0o0p committed Aug 16, 2024
1 parent adf731b commit 9bc7733
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
Expand All @@ -25,15 +24,14 @@ public class TeacherLoginService implements TeacherLoginUseCase {
private final XquareClient xquareClient;
private final PasswordEncoder passwordEncoder;

@Transactional
public TokenResponse teacherLogin(LoginRequest request) {
return teacherMongoRepository.existsByAccountId(request.account_id())
return teacherMongoRepository.existsByAccountId(request.account_id().trim())
? loginExistingTeacher(request)
: registerAndLoginNewTeacher(request);
}

private TokenResponse loginExistingTeacher(LoginRequest request) {
TeacherEntity teacher = teacherMongoRepository.findFirstByAccountId(request.account_id())
TeacherEntity teacher = teacherMongoRepository.findByAccountId(request.account_id())
.orElseThrow(() -> TeacherNotFoundException.EXCEPTION);

if (!passwordEncoder.matches(request.password(), teacher.getPassword())) {
Expand All @@ -46,13 +44,14 @@ private TokenResponse loginExistingTeacher(LoginRequest request) {
private TokenResponse registerAndLoginNewTeacher(LoginRequest request) {
XquareUserResponse xquareUserResponse = xquareClient.xquareUser(request);
TeacherEntity newTeacher = createAndSaveNewTeacher(xquareUserResponse);

return jwtTokenProvider.receiveToken(newTeacher.getId(), UserRole.TEACHER);
}

private TeacherEntity createAndSaveNewTeacher(XquareUserResponse xquareUserResponse) {
return teacherMongoRepository.save(
TeacherEntity.builder()
.account_id(xquareUserResponse.getAccount_id())
.accountId(xquareUserResponse.getAccount_id())
.password(xquareUserResponse.getPassword())
.name(xquareUserResponse.getName())
.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
import java.util.Optional;

public interface TeacherMongoRepository extends MongoRepository<TeacherEntity, String> {
Optional<TeacherEntity> findFirstByAccountId(String account_id);
Boolean existsByAccountId(String account_id);
Optional<TeacherEntity> findByAccountId(String accountId);
Boolean existsByAccountId(String accountId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public class TeacherEntity {
private String password;

@Builder
public TeacherEntity(String name, String account_id, String password) {
public TeacherEntity(String name, String accountId, String password) {
this.name = name;
this.accountId = account_id;
this.accountId = accountId;
this.password = password;
}
protected TeacherEntity() {}
Expand Down

0 comments on commit 9bc7733

Please sign in to comment.