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

fix: fix login and register api to return response instead of throwin… #26

Merged
merged 1 commit into from
Apr 28, 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
import com.github.nhatoriginal.spring.model.Role;
import com.github.nhatoriginal.spring.model.User;
import com.github.nhatoriginal.spring.repository.UserRepository;

import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.AuthenticationManager;

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;

@Service
public class AuthService {
Expand All @@ -33,7 +36,7 @@ public AuthService(UserRepository userRepository, PasswordEncoder passwordEncode
public String register(AuthRegisterDto authRegisterDto) {
System.out.println(authRegisterDto);
if (userRepository.findByEmail(authRegisterDto.getEmail()) != null) {
throw new IllegalArgumentException("Email already in use");
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "Email already exists");
}

User user = User.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,39 @@
import com.github.nhatoriginal.spring.repository.UserRepository;
import com.github.nhatoriginal.spring.security.CustomUserDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;

import java.util.UUID;

@Service
public class UserService implements UserDetailsService {

private final UserRepository userRepository;

private final UserRepository userRepository;
public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
}

public UserService(UserRepository userRepository) {
this.userRepository = userRepository;
@Override
public UserDetails loadUserByUsername(String email) {
User user = userRepository.findByEmail(email);
if (user == null) {
throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "email does not exists");
}
return new CustomUserDetails(user);
}

@Override
public UserDetails loadUserByUsername(String email) {
User user = userRepository.findByEmail(email);
if (user == null) {
throw new UsernameNotFoundException(email);
}
return new CustomUserDetails(user);
}
public User findByEmail(String email) {
return userRepository.findByEmail(email);
}
public User getUserById(String id) {
return userRepository.findById(UUID.fromString(id)).orElse(null);
}
public User findByEmail(String email) {
return userRepository.findByEmail(email);
}

public User getUserById(String id) {
return userRepository.findById(UUID.fromString(id)).orElse(null);
}

}
Loading