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

Feature/#168 로그인api 반환 값 변경 #169

Merged
merged 6 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
Expand Up @@ -3,14 +3,17 @@

import jakarta.transaction.Transactional;
import java.security.SecureRandom;
import java.util.Optional;
import java.util.regex.Pattern;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import sw_css.auth.application.dto.response.SignInResponse;
import sw_css.auth.exception.AuthException;
import sw_css.auth.exception.AuthExceptionType;
import sw_css.member.domain.FacultyMember;
import sw_css.member.domain.Member;
import sw_css.member.domain.Role;
import sw_css.member.domain.StudentMember;
import sw_css.member.domain.embedded.Password;
import sw_css.member.domain.repository.FacultyMemberRepository;
import sw_css.member.domain.repository.MemberRepository;
Expand Down Expand Up @@ -38,12 +41,23 @@ public SignInResponse signIn(String email, String rawPassword) {
throw new AuthException(AuthExceptionType.MEMBER_WRONG_ID_OR_PASSWORD);
}

String role = loadMemberRole(member);
boolean isModerator = role.equals(Role.ROLE_ADMIN.toString());
Object memberDetail = loadMemberDetail(member);
if (memberDetail instanceof StudentMember studentMember) {
String role = Role.ROLE_MEMBER.toString();

String accessToken = jwtTokenProvider.createToken(member.getId(), role);
String accessToken = jwtTokenProvider.createToken(member.getId(), role);

return SignInResponse.of(member, role, isModerator, accessToken);
return SignInResponse.of(member, studentMember.getId(), role, false, accessToken);

} else if (memberDetail instanceof FacultyMember facultyMember) {
String role = Role.ROLE_ADMIN.toString();

String accessToken = jwtTokenProvider.createToken(member.getId(), role);

return SignInResponse.of(member, facultyMember.getId(), role, true, accessToken);
}
Comment on lines +45 to +58
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분에서 if문을 사용하지 않고 할 수 있는 방법이 있을까요?
코드가 약간 더러운 느낌이라서요...
아는 방법있으면 알려주세용!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

고민을 해봤는데 지금 구조상 지금 방법이 최선인 것 같습니다!


throw new AuthException(AuthExceptionType.MEMBER_EMAIL_NOT_FOUND);
}

public void resetPassword(String email, String name) {
Expand All @@ -63,13 +77,16 @@ public void resetPassword(String email, String name) {
authEmailService.sendNewPassword(email, newPassword);
}

private String loadMemberRole(Member member) {
private Object loadMemberDetail(Member member) {
Long memberId = member.getId();
if (studentMemberRepository.existsByMemberId(memberId)) {
return Role.ROLE_MEMBER.toString();
Optional<StudentMember> studentMember = studentMemberRepository.findByMemberId(memberId);
if (studentMember.isPresent()) {
return studentMember.get();
}
if (facultyMemberRepository.existsByMemberId(memberId)) {
return Role.ROLE_ADMIN.toString();

Optional<FacultyMember> facultyMember = facultyMemberRepository.findByMemberId(memberId);
if (facultyMember.isPresent()) {
return facultyMember.get();
}
throw new AuthException(AuthExceptionType.MEMBER_NOT_FOUND);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public record SignInResponse(
String token
) {

public static SignInResponse of(Member member, String role, Boolean isModerator, String token) {
return new SignInResponse(member.getId(), member.getName(), member.getEmail(), role, isModerator, token);
public static SignInResponse of(Member member, long member_id, String role, Boolean isModerator, String token) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

사소한거긴 한데 객체로 다룰 수 있는 Long 타입을 사용해주시면 좋을 것 같습니다!
그리고 변수명은 카멜케이스로 부탁드립니다! (memberId)

return new SignInResponse(member_id, member.getName(), member.getEmail(), role, isModerator, token);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package sw_css.member.domain.repository;

import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import sw_css.member.domain.FacultyMember;

public interface FacultyMemberRepository extends JpaRepository<FacultyMember, Long> {
boolean existsByMemberId(Long memberId);

Optional<FacultyMember> findByMemberId(Long memberId);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package sw_css.member.domain.repository;

import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import sw_css.member.domain.StudentMember;

public interface StudentMemberRepository extends JpaRepository<StudentMember, Long> {
boolean existsById(Number studentId);

boolean existsByMemberId(Number memberId);

Optional<StudentMember> findByMemberId(Number memberId);
}
15 changes: 7 additions & 8 deletions backend/src/main/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ drop table if exists sw_css.milestone_history;

create table member
(
id bigint auto_increment primary key,
email varchar(255) not null,
name varchar(255) not null,
password varchar(255) not null,
phone_number varchar(255) not null,
is_authorized boolean not null,
is_deleted boolean not null,
created_at datetime(6) not null default current_timestamp(6)
id bigint auto_increment primary key,
email varchar(255) not null,
name varchar(255) not null,
password varchar(255) not null,
phone_number varchar(255) not null,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is_authorized 값은 필요하지 않은 값인가요??👀

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

넹.
이전에는 메일인증을 회원가입 다하고 난 뒤에 진행하는 방식이라서 메일 인증이 완료되었는지 필요했습니다.
하지만 지금은 회원가입 단계에서 메일인증을 받으므로 필요가 없어졌습니다.

혹시 다른 일 관련으로 사용처가 있었을까요?

is_deleted boolean not null,
created_at datetime(6) not null default current_timestamp(6)
);

create table student_member
Expand Down
6 changes: 3 additions & 3 deletions backend/src/main/resources/test-data.sql
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
## member(student)
insert into member (email, name, password, phone_number, is_authorized, is_deleted)
insert into member (email, name, password, phone_number, is_deleted)
values ('[email protected]', '송세연', '$2a$10$YyiOL/E5WjKrZPkB6eQSK.PwZtAO.z3JimFbq/Ky3u3rFf3XTGrWK', '010-0000-0000',
true, false);

insert into student_member (id, member_id, major_id, minor_id, double_major_id, career, career_detail)
values (202055558, 1, 1, null, null, 'EMPLOYMENT_COMPANY', 'IT 기업 개발자');

insert into member (email, name, password, phone_number, is_authorized, is_deleted)
insert into member (email, name, password, phone_number, is_deleted)
values ('[email protected]', '이다은', '$2a$10$YyiOL/E5WjKrZPkB6eQSK.PwZtAO.z3JimFbq/Ky3u3rFf3XTGrWK', '010-0000-0000',
true, false);

insert into student_member (id, member_id, major_id, minor_id, double_major_id, career, career_detail)
values (202055555, 2, 1, null, null, 'GRADUATE_SCHOOL', 'IT 기업 개발자');

insert into member (email, name, password, phone_number, is_authorized, is_deleted)
insert into member (email, name, password, phone_number, is_deleted)
values ('[email protected]', '김하윤', '$2a$10$YyiOL/E5WjKrZPkB6eQSK.PwZtAO.z3JimFbq/Ky3u3rFf3XTGrWK', '010-0000-0000',
true, false);

Expand Down