I’m using python 3.11.1 and of course, i can give you all that you need.
Installed Apps:
INSTALLED_APPS = [
"corsheaders",
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'blackportal',
'rest_framework',
'django_filters',
'drf_yasg',
'rest_framework_simplejwt'
],
AUTHENTICATION_BACKENDS = [
'blackportal.controllers.user.UserController.EmailUsernameAuthenticationBackend'
]
User Model:
class User(AbstractUser):
code = models.CharField(max_length=12, unique=True)
role = models.CharField(max_length=50,null=True,blank=True, default=None)
name = models.CharField(max_length=100)
last_name = models.CharField(max_length=150)
username = models.CharField(max_length=150, unique=True)
dui = models.CharField(max_length=20, unique=True)
short_name = models.CharField(max_length=50, unique=True)
position = models.CharField(max_length=100)
email = models.EmailField(max_length=75, unique=True)
password = models.TextField()
system_access = models.TextField(null=True, blank=True, default=None)
deleted = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
deleted_at = models.DateTimeField(blank=True, null=True)
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
Others:
class EmailUsernameAuthenticationBackend(object):
@staticmethod
def authenticate(request, username=None, password=None):
try:
user = User.objects.get(
Q(username=username)
)
except User.DoesNotExist:
return None
if user and check_password(password, user.password):
return user
return None
@staticmethod
def find_user(username=None):
try:
return User.objects.get(
Q(username=username)
)
except User.DoesNotExist:
return 'User not found'
Login View:
class LoginApiView(APIView):
def post(self,request):
login_data = request.data
userdata = login_data.get('username','')
password = login_data.get('password','')
user_auth = AuthT.authenticate(request,username=userdata, password=password)
print(user_exists)
if user_auth:
login_serializer = self.serializer_class(data=request.data)
if login_serializer.is_valid():
print(login_serializer.validated_data)
return Response({
"access_token":login_serializer.validated_data.get('access'),
"refresh_token":login_serializer.validated_data.get('refresh')
},status=status.HTTP_200_OK)
else:
return Response({"error":login_serializer.errors}, status=status.HTTP_400_BAD_REQUEST)
else:
return Response({"message":"Invalid credentials"}, status=status.HTTP_400_BAD_REQUEST)
CutomTokenSerializer:
class CustoMObtainPairSerializer(TokenObtainPairSerializer):
@classmethod
def get_token(cls, user):
token = super().get_token(user)
token['email'] = user.email
token['code'] = user.code
token['dui'] = user.dui
token['role'] = user.role
token['short_name'] = user.short_name
token['username'] = user.username
return token
This is my code for authentication and everything related to user administration (I think CRUD operations are not necessary, i guess )
I really appreciate your help