moving to docker
This commit is contained in:
0
api/app/custom_auth/__init__.py
Normal file
0
api/app/custom_auth/__init__.py
Normal file
3
api/app/custom_auth/admin.py
Normal file
3
api/app/custom_auth/admin.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
6
api/app/custom_auth/apps.py
Normal file
6
api/app/custom_auth/apps.py
Normal file
@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class AuthConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "custom_auth"
|
||||
0
api/app/custom_auth/migrations/__init__.py
Normal file
0
api/app/custom_auth/migrations/__init__.py
Normal file
3
api/app/custom_auth/models.py
Normal file
3
api/app/custom_auth/models.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
3
api/app/custom_auth/tests.py
Normal file
3
api/app/custom_auth/tests.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
8
api/app/custom_auth/urls.py
Normal file
8
api/app/custom_auth/urls.py
Normal file
@ -0,0 +1,8 @@
|
||||
from django.urls import path, include
|
||||
from custom_auth import views
|
||||
|
||||
urlpatterns = [
|
||||
path('', include('rest_framework.urls')),
|
||||
path('token/', views.CustomAuthToken.as_view()),
|
||||
path('register/', views.RegisterView.as_view())
|
||||
]
|
||||
55
api/app/custom_auth/views.py
Normal file
55
api/app/custom_auth/views.py
Normal file
@ -0,0 +1,55 @@
|
||||
from rest_framework.authtoken.views import ObtainAuthToken
|
||||
from rest_framework.authtoken.models import Token
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.request import Request
|
||||
from rest_framework.views import APIView
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
class CustomAuthToken(ObtainAuthToken):
|
||||
|
||||
def post(self, request: Request, *args, **kwargs):
|
||||
serializer = self.serializer_class(data=request.data,
|
||||
context={'request': request})
|
||||
serializer.is_valid(raise_exception=True)
|
||||
user = serializer.validated_data['user']
|
||||
token, created = Token.objects.get_or_create(user=user)
|
||||
return Response({
|
||||
'token': token.key,
|
||||
'user_id': user.pk,
|
||||
'username': user.username
|
||||
})
|
||||
|
||||
|
||||
class RegisterView(APIView):
|
||||
|
||||
def get(self, request: Request, *args, **kwargs):
|
||||
username = request.data.get("username", None)
|
||||
if not username:
|
||||
return Response({"message": "Username must not be empty"}, status=406)
|
||||
if User.objects.filter(username=username).exists():
|
||||
return Response({"message": "Username already taken"}, status=409)
|
||||
return Response({"message": "Username availiable"}, status=200)
|
||||
|
||||
def post(self, request: Request, *args, **kwargs):
|
||||
username = request.data.get("username")
|
||||
if not username:
|
||||
return Response({"message": "Username must not be empty"}, status=406)
|
||||
if User.objects.filter(username=username).exists():
|
||||
return Response({"message": "Username already taken"}, status=409)
|
||||
password = request.data.get("password", None)
|
||||
if not password:
|
||||
return Response({"message": "Password must not be empty"}, status=406)
|
||||
try:
|
||||
user = User.objects.create_user(
|
||||
is_superuser=False,
|
||||
username=username,
|
||||
password=password
|
||||
)
|
||||
token, created = Token.objects.get_or_create(user=user)
|
||||
return Response({
|
||||
'token': token.key,
|
||||
'user_id': user.pk,
|
||||
'username': user.username
|
||||
}, status=200)
|
||||
except Exception as e:
|
||||
return Response({"message": "Something went wrong, registration is not completed", "error": e}, status=500)
|
||||
Reference in New Issue
Block a user