I'm working on a project using ReactJS as the frontend, deployment on AWS Amplify, and Spring Boot as the backend deployed on an AWS instance. Some users are getting this error when they try to register:
NetworkError:axios Error
Yet for other users it works fine.
here is my code:
registration.js
static BASE_URL = ";;
static async register(userData) {
console.log(JSON.stringify(userData));
try {
const response = await axios.post(
`${UserService.BASE_URL}/auth/register`,
userData,
{
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
}
);
console.log(response.data);
return response.data;
} catch (err) {
throw err;
}
}
my spring boot backend:
@Bean
CorsConfigurationSource corsConfigurationSource(){
CorsConfiguration configuration=new CorsConfiguration();
configuration.setAllowedOrigins(List.of(";));
configuration.setAllowedMethods(Arrays.asList("GET", "POST", "PUT", "DELETE","HEAD","OPTIONS"));
configuration.setAllowedHeaders(Arrays.asList(HttpHeaders.AUTHORIZATION
,HttpHeaders.CONTENT_TYPE
,HttpHeaders.ACCEPT));
configuration.setAllowCredentials(true); // Allow credentials
configuration.setMaxAge(3600L); // Optional: Set max age for preflight requests
configuration.addAllowedHeader("*");
UrlBasedCorsConfigurationSource source=new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**",configuration);
return source;
}