Skip to content

Commit

Permalink
Merge pull request #26 from Nhat-Original/fix/fix-auth-response
Browse files Browse the repository at this point in the history
fix: fix login and register api to return response instead of throwin…
  • Loading branch information
Nhat-Original authored Apr 28, 2024
2 parents b87f6ce + 1a48e86 commit bfc103c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
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);
}

}

0 comments on commit bfc103c

Please sign in to comment.