Axios post request not sending data or recieving response

Viewed 45

This is my first time trying to do basic authentication and nothing seems to be working. Axios post is not sending data/controllers not returning a response. I cant find exactly where I am going wrong, but I am wondering if the body of my http post request has to match requestbody or what I need to change to get userrepositroy to save form dat in database?

const Login = () => {

    const [registration, setRegistration] = useState("true");

    const [email, setEmail] = useState("");
    const [regPwd, setRegPwd] = useState("");
    const [regUsername, setRegUsername] = useState("");

    const [logUsername, setLogUsername] = useState("");
    const [logPwd, setLogPwd] = useState("");

    const redirect = useNavigate();

    const handleRegSubmit = async (e) => {
        e.preventDefault();
        const registerDTO = {
            email: email,
            username: regUsername,
            password: regPwd
        }
        try {
            const response = await axios.post('http://localhost:8080/registration',
                 {registerDTO} )
                if (response.status === 200) {
                    redirect("/user/${username}")
                } else {
                    setEmail("")
                    setRegUsername("")
                    setRegPwd("")
                }
        }
        catch (error) {
            console.log(error)
        }
    }

    const handleLogSubmit = async (e) => {
        e.preventDefault();
        const logUser = {
            username: logUsername,
            password: logPwd
        }
        try {
            const response = await axios.post('http://localhost:8080/login',
                { logUser })
                if (response.status === 200) {
                    redirect("/user/${username}")
                } else {
                    setLogUsername("")
                    setLogPwd("")
                }
        }
        catch (error) {
            console.log(error)
        };   
    }




Spring boot



@CrossOrigin(origins= "http://localhost:3000")
@RestController
@RequestMapping("")
public class AuthenticationController {

    @Autowired
    UserRepository userRepository;

       
    @PostMapping("/register")
    public ResponseEntity<Object> processRegistration(@RequestBody @Valid RegisterDTO registerDTO,
                                                      BindingResult bindingResult, HttpServletRequest request) {

        if (bindingResult.hasErrors()) {
            return ResponseEntity.badRequest().body("An error occurred!");
        }

        User existingUser = userRepository.findByUsername(registerDTO.getUsername());

        if (existingUser != null) {
            return ResponseEntity.badRequest().body("Username already exists!");
        }


        User user = new User(registerDTO.getEmail(), registerDTO.getUsername(), registerDTO.getPassword());
        userRepository.save(user);

        return ResponseEntity.ok().body(user);
    }

    @PostMapping("/login")
    public ResponseEntity<Object> processLogin(@RequestBody @Valid LoginDTO loginDTO,
                                               BindingResult bindingResult,HttpServletRequest request) {

        if (bindingResult.hasErrors()) {
            return ResponseEntity.badRequest().body("An error occurred!");
        }

        User user = userRepository.findByUsername(loginDTO.getUsername());

        if (user == null) {
            return ResponseEntity.badRequest().body("Username does not exist!");
        }

        String password = loginDTO.getPassword();

        if (!user.isMatchingPassword(password)) {
            return ResponseEntity.badRequest().body("Invalid password!");
        }

        return ResponseEntity.ok(user);
    }

}

1 Answers

You're sending to http://localhost:8080/registration but defined the route as localhost:8080/register

Related