Django - CSRF Failed: CSRF cookie not set
I want to create a API that has login / logout / signup / edit etc, but when I try it in Postman i get the error: CSRF Failed: CSRF cookie not set
I've noticed that if the user is not logged in, The API works fine but after the login I get the CSRF error when using any url from it ( /logout/, /login/ etc)
views.py
`@csrf_exempt
@api_view(['POST'])
def login_user(request):
if request.user.is_authenticated:
return JsonResponse({"detail": "User is logged in!"})
body_unicode = request.body.decode('utf-8')
body = json.loads(body_unicode)
nickname = body['nickname']
password = body['password']
user = authenticate(nickname=nickname, password=password)
if user is not None:
login(request, user)
logout(request)
return JsonResponse({"response": "Login success!"})
else:
return JsonResponse({"error": "Nickname or Password are incorrect!"})`
settings.py
`MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]`
I've seen that I should get a CSRF token in the cookies section in Postman, but the only thing i get is a sessionId