Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added auth/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions auth/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
6 changes: 6 additions & 0 deletions auth/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class AuthConfig(AppConfig):
default_auto_field = "django.db.models.BigAutoField"
name = "auth"
Empty file added auth/migrations/__init__.py
Empty file.
3 changes: 3 additions & 0 deletions auth/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.db import models

# Create your models here.
3 changes: 3 additions & 0 deletions auth/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
7 changes: 7 additions & 0 deletions auth/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.urls import path

from auth.views import login

urlpatterns = [
path("", login),
]
27 changes: 27 additions & 0 deletions auth/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from django.contrib.auth import authenticate
from django.views.decorators.csrf import csrf_exempt
from rest_framework.authtoken.models import Token
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import AllowAny
from rest_framework.status import HTTP_400_BAD_REQUEST, HTTP_404_NOT_FOUND, HTTP_200_OK
from rest_framework.response import Response


@csrf_exempt
@api_view(["POST"])
@permission_classes((AllowAny,))
def login(request):
username = request.data.get("username")
password = request.data.get("password")
print(username, password)
if username is None or password is None:
return Response(
{"error": "Please provide both username and password"},
status=HTTP_400_BAD_REQUEST,
)
user = authenticate(username=username, password=password)
print(user)
if not user:
return Response({"error": "Invalid Credentials"}, status=HTTP_404_NOT_FOUND)
token, _ = Token.objects.get_or_create(user=user)
return Response({"token": token.key}, status=HTTP_200_OK)
24 changes: 24 additions & 0 deletions settings_config/common.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import os
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Application definition
INSTALLED_APPS = [
"django.contrib.admin",
Expand All @@ -9,8 +16,17 @@
"django_celery_beat",
"corsheaders",
"rest_framework",
"rest_framework.authtoken",
]


REST_FRAMEWORK = {
"DEFAULT_AUTHENTICATION_CLASSES": (
"rest_framework.authentication.TokenAuthentication",
),
"DEFAULT_PERMISSION_CLASSES": ("rest_framework.permissions.IsAuthenticated",),
}

MIDDLEWARE = [
"corsheaders.middleware.CorsMiddleware",
"django.middleware.common.CommonMiddleware",
Expand Down Expand Up @@ -39,6 +55,14 @@
},
]

DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}


LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"
Expand Down
1 change: 1 addition & 0 deletions settings_config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@
path("dydx_operations/", include("dydx_operations.urls")),
path("cruize_operations/", include("cruize_operations.urls")),
path("market_data/", include("market_data.urls")),
path("auth/", include("auth.urls")),
]