Django Djoser not working in production return 405 using react as frontend

27 Views Asked by At

In my application, I'm using Django as my backend and react JS for frontend. I'm using Django Djoser for my user activation and reset password. Locally, everything works fine. After creating new user, activation link will be sent to the user email

http://127.0.0.1/activate/MjE1/c1vk2z-0cf8bc5d2484ddd32001844b53da1900

If i'm using my domain https://workmatch.rickykristianbutarbutar.com/activate/MjE1/c1vk2z-0cf8bc5d2484ddd32001844b53da1900 then 405 comes out

after clicking this link, it will navigate to the frontend activation page, then user click "Activate"

const sendActivationConfirmation = async () => {
    setButtonLabel("Activating ...");
    try {
        let response = await fetch(`/auth/users/activation/`, {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify({
                uid: uid,
                token: token
            })
        });

        if (response.ok) {
            setAlert({
                success: "Your account has been activated. We'll redirect you to the login page in 5 seconds.",
            });
            activationSuccess.current = true;
        } else if (response.status === 403) {
            let data = await response.json();
            setAlert({
                error: data.error
            });
        } else if (response.status === 400) {
            setAlert({
                error: "Bad Request, user may have been accidentally deleted."
            });
        }
    } catch (error) {
        if (error.name === "SyntaxError" && error.message.includes("Unexpected end of JSON input")) {
            console.error("Truncated data: Not all of the JSON data was received");
        } else {
            console.error(error);
        }
    } finally {
        setButtonLabel("Activate");
    }
};

this code will be an activation code and send it to the server

This is my Djoser settings in my Django settings.py

DJOSER = {
    "USER_ID_FIELD": "username",
    "LOGIN_FIELD": "email",
    "USER_CREATE_PASSWORD_RETYPE": True,
    "USERNAME_CHANGED_EMAIL_CONFIRMATION": True,
    "PASSWORD_CHANGED_EMAIL_CONFIRMATION": True,
    "SEND_CONFIRMATION_EMAIL": True,
    "SET_USERNAME_RETYPE": True,
    "SET_PASSWORD_RETYPE": True,
    "PASSWORD_RESET_CONFIRM_URL": "#/password/reset/confirm/{uid}/{token}",
    "USERNAME_RESET_CONFIRM_URL": "#/email/reset/confirm/{uid}/{token}",
    "ACTIVATION_URL": "activate/{uid}/{token}",
    "SEND_ACTIVATION_EMAIL": True,
    "SERIALIZERS": {
        "activation": "djoser.serializers.ActivationSerializer",
        "user_create": "userApi.serializers.UserCreateSerializer",
        "user": "userApi.serializers.UserCreateSerializer",
        "user_delete": "djoser.serializer.UserDeleteSerializer",
    },
}
enter code here

And this is my urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/user/', include('userApi.urls')),

    # JWT authentication
    re_path(r'^auth/', include('djoser.urls')),
    re_path(r'^auth/', include('djoser.urls.jwt')),

    path('ckeditor/', include('ckeditor_uploader.urls')),
]

Everything works fine until i deploy it to my server production Is there any setting to configure on my server side? I'm using Ngix and gunicorn

this is my nginx configuration

server {

    root /root/WorkMatch/frontend/HiringRemoteWorker-Frontend/build/;
    index index.html index.htm /index.html;

    server_name workmatch.rickykristianbutarbutar.com;

    location /static/ {
         alias /root/WorkMatch/frontend/HiringRemoteWorker-Frontend/build/static/;
    }

    location / {
        try_files $uri $uri/ /index.html;
    }

    location /api/ {
        # Proxy requests to Gunicorn/Django backend
        proxy_pass http://127.0.0.1:8001;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location ~ ^/admin {
        proxy_pass http://unix:/root/WorkMatch/backend/HiringRemoteWorker/HiringRemoteWorker.sock;
        proxy_set_header HOST $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

    location /django-static {
        autoindex on;
        alias /root/WorkMatch/backend/HiringRemoteWorker/staticfiles;
    }

    location /media/ {
        alias /root/WorkMatch/backend/HiringRemoteWorker/media/profile_picture/;
    }

}

0

There are 0 best solutions below