@csrf_exempt
@api_view(['POST'])
@permission_classes([permissions.AllowAny])
def api_authenticate(request):
    user = None
    data = None
    status_code = 400
    error = None

    try:
        data = json.loads(request.body.decode())
    except ValueError:
        error = "Could not parse body as JSON"

    if not error:
        try:
            email = data['email']
            password = data['password']
            user = auth.authenticate(username=email, password=password)
            if not user:
                status_code = 401
                error = "Invalid credentials"
        except KeyError as e:
            error = "Missing required field '%s'" % e.args[0]

    if user:
        return JsonResponse({'token': user.auth_token.key})

    return JsonResponse({'error': error}, status=status_code)
dataset/ETHPy150Open jessamynsmith/eggtimer-server/periods/views.py/api_authenticate/original