Skip to content

Commit

Permalink
✨ feat: 자체 login 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
tmddus2 committed Aug 24, 2024
1 parent b069709 commit 248ed85
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.example.purithm.domain.user.repository;

import java.util.Optional;

import com.example.purithm.domain.user.entity.Provider;
import com.example.purithm.domain.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
Expand All @@ -19,4 +21,6 @@ public interface UserRepository extends JpaRepository<User, Long> {
int countLogsByUserId(@Param("userId") Long userId);

boolean existsByProviderId(String id);

Optional<User> findByProviderId(String id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,14 @@ public void updateProfile(UserInfoRequestDto userInfo, Long userId) {
user.updateProfile(userInfo);
userRepository.save(user);
}

public Long getUserId(String id, String password) {
User user = userRepository.findByProviderId(id)
.orElseThrow(() -> CustomException.of(Error.NOT_FOUND_ERROR));

if (!user.getPassword().equals(password)) {
throw CustomException.of(Error.INVALID_ID_PASSWORD);
}
return user.getId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ public class AuthController implements AuthControllerDocs {

@PostMapping("/login")
public SuccessResponse<LoginDto> login(LoginRequestDto loginRequestDto) {
Long id = userService.getUserId(loginRequestDto.id(), passwordEncoder.encode(loginRequestDto.password()));
String jwtToken = jwtUtil.createJwt(id, 60 * 60 * 60 * 1000L);

LoginDto loginDto = LoginDto.builder()
.accessToken(jwtToken).build();
return SuccessResponse.of(loginDto);
}

@PostMapping("/signup")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public enum Error {
/* 401 */
INVALID_TOKEN_ERROR(HttpStatus.UNAUTHORIZED, 40100, "유효하지 않은 토큰입니다."),
EXPIRED_TOKEN_ERROR(HttpStatus.UNAUTHORIZED, 40101, "만료된 토큰입니다."),
INVALID_ID_PASSWORD(HttpStatus.UNAUTHORIZED, 40102, "이메일/비밀번호가 적절하지 않습니다."),

/* 403 */
NOT_AGREED_TERM(HttpStatus.FORBIDDEN, 40300, "이용약관 동의가 필요합니다"),
Expand Down

0 comments on commit 248ed85

Please sign in to comment.