JWT token not being sent in production (Django REST Framework & Next.js 14) - Stack Overflow

I'm facing an issue where JWT tokens are not being sent from the client to the server in my produc

I'm facing an issue where JWT tokens are not being sent from the client to the server in my production environment. The project consists of a Django REST Framework backend and a Next.js frontend. Everything works fine in the development (127.0.0.1) environment, but in production, the JWT token stored in the cookies is not being sent back to the server.

Project Setup: Backend (Django) Settings: Here are my relevant settings in settings.py:

REST_AUTH = {
    'JWT_AUTH_COOKIE': 'token',
    'JWT_AUTH_REFRESH_COOKIE': 'refresh_token',
    'JWT_AUTH_SECURE': True,  # Enabled for production
    'JWT_AUTH_HTTPONLY': True,
    'JWT_AUTH_SAMESITE': 'None',
}

CORS_ALLOW_CREDENTIALS = True
CORS_ALLOWED_ORIGINS = [
    '',  # My frontend domain
]

Frontend (Next.js) Configuration: On the client side, I'm sending requests using fetch as follows:

fetch("/auth/login", {
    method: "POST",
    credentials: "include",  // Ensure cookies are sent
    headers: {
        "Content-Type": "application/json"
    },
    body: JSON.stringify({ username, password })
});

Environment: Frontend URL: Backend URL: Browser: Chrome (latest version) HTTPS is enabled for both frontend and backend.

Why aren't the cookies being sent in subsequent requests? What could be causing this issue, and how can I fix it?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745664407a4639037.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信