CSRF Issue when Submitting Form via React Native + Expo to Django Backend in Production

40 Views Asked by At

I'm currently navigating my first experience in app development, I'm facing an issue while developing an application that submits a simple form to a Django backend. When sending the POST request locally, everything works perfectly. However, upon deploying the application to production, the following error occurs (OBS: I'm configuring a Django API where a certain endpoint does not require authentication for a POST request. Initially, I want Django to accept the request without authentication, and the permission class is set to AllowAny):

Error submitting the form:, Object {
      "detail": "CSRF Failed: Referer checking failed - no Referer.",
    }

I've configured the Django settings as follows:

CSRF_COOKIE_SECURE = False
CSRF_COOKIE_SAMESITE = None
CSRF_USE_SESSIONS = False

CORS_ALLOW_CREDENTIALS = True
    const handleSubmit = () => {
    const formData = new FormData();
    formData.append("nome", nome);
   
    axios
        //.post('mydomain/apitest/registertest/', formData, {
        .post('http://10.0.2.2:8002/apitest/registertest/', formData, {
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        })
        .then(response => {
            console.log('Answ API:', response.data);

            const { message } = response.data;

            if (message === "Success!") {
            
                navigation.navigate('Production', { id_user: id_user, token: token })
            } else {
                console.log('Error:', message);
               
            }
        })
        .catch(error => {
            console.error('Error:', error.response ? error.response.data : error.message);
            console.log(JSON.stringify(error, null, 2))
            
        });
};
0

There are 0 best solutions below