-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Completed authentication view and test
- Loading branch information
1 parent
e9e5bdc
commit 88992b1
Showing
16 changed files
with
726 additions
and
271 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from django.contrib.auth import get_user_model | ||
|
||
# User | ||
User = get_user_model() | ||
|
||
|
||
# ?is_exists_wallet_address | ||
def is_exists_wallet_address(wallet_address: str): | ||
if User.objects.filter(wallet_address=wallet_address).exists(): | ||
return True | ||
return False |
27 changes: 27 additions & 0 deletions
27
backend/apps/users/migrations/0008_alter_customuser_wallet_address.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Generated by Django 5.0.1 on 2024-03-25 18:34 | ||
|
||
from django.db import migrations, models | ||
|
||
import config.helpers | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("users", "0007_initial"), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name="customuser", | ||
name="wallet_address", | ||
field=models.CharField( | ||
db_index=True, | ||
max_length=100, | ||
null=True, | ||
unique=True, | ||
validators=[config.helpers.is_valid_wallet_address], | ||
verbose_name="Wallet address", | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
from django.contrib.auth import authenticate | ||
from rest_framework import serializers | ||
|
||
from apps.transaction.validate_field import ( | ||
validate_confirmations, | ||
validate_wallet_address, | ||
) | ||
|
||
from .helpers import is_exists_wallet_address | ||
from .models import CustomUser | ||
|
||
|
||
# !CustomUserSerializer | ||
class CustomUserSerializer(serializers.ModelSerializer): | ||
""" | ||
Serializer class to serialize CustomUser model. | ||
""" | ||
|
||
class Meta: | ||
model = CustomUser | ||
fields = ("id", "username", "email", "wallet_address") | ||
|
||
|
||
# !UserRegisterationSerializer | ||
class UserRegisterationSerializer(serializers.ModelSerializer): | ||
""" | ||
Serializer class to serialize registration requests and create a new user. | ||
""" | ||
|
||
wallet_address = serializers.CharField(validators=[validate_wallet_address]) | ||
|
||
def validate_wallet_address(self, wallet_address): | ||
if not validate_wallet_address(wallet_address): | ||
raise serializers.ValidationError("Please input valid wallet address") | ||
elif is_exists_wallet_address(wallet_address): | ||
raise serializers.ValidationError("This wallet address already set other") | ||
return wallet_address | ||
|
||
class Meta: | ||
model = CustomUser | ||
fields = ("id", "username", "email", "password", "wallet_address") | ||
extra_kwargs = {"password": {"write_only": True}} | ||
|
||
def create(self, validated_data): | ||
return CustomUser.objects.create_user(**validated_data) | ||
|
||
|
||
# !UserLoginSerializer | ||
class UserLoginSerializer(serializers.Serializer): | ||
""" | ||
Serializer class to authenticate users with email and password. | ||
""" | ||
|
||
email = serializers.CharField() | ||
password = serializers.CharField(write_only=True) | ||
|
||
def validate(self, data): | ||
user = authenticate(**data) | ||
if user and user.is_active: | ||
return user | ||
raise serializers.ValidationError("Incorrect Credentials") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,21 @@ | ||
from django.urls import path | ||
from rest_framework_simplejwt.views import TokenRefreshView | ||
|
||
from .views import ( | ||
UserAPIView, | ||
UserLoginAPIView, | ||
UserLogoutAPIView, | ||
UserRegisterationAPIView, | ||
) | ||
|
||
# from .views import hello_world | ||
|
||
|
||
app_name = "users" | ||
# urlpatterns = [path("sayhello/", hello_world, name="hello_world")] | ||
urlpatterns = [ | ||
path("register/", UserRegisterationAPIView.as_view(), name="create-user"), | ||
path("login/", UserLoginAPIView.as_view(), name="login-user"), | ||
path("token/refresh/", TokenRefreshView.as_view(), name="token-refresh"), | ||
path("logout/", UserLogoutAPIView.as_view(), name="logout-user"), | ||
path("", UserAPIView.as_view(), name="user-info"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,87 @@ | ||
from django.shortcuts import render | ||
from rest_framework.decorators import api_view | ||
from django.contrib.auth import get_user_model | ||
from rest_framework import status | ||
from rest_framework.generics import GenericAPIView, RetrieveUpdateAPIView | ||
from rest_framework.permissions import AllowAny, IsAuthenticated | ||
from rest_framework.response import Response | ||
from rest_framework_simplejwt.tokens import RefreshToken | ||
|
||
# Create your views here. | ||
from apps.user_profile.models import Profile | ||
|
||
from .serializers import ( | ||
CustomUserSerializer, | ||
UserLoginSerializer, | ||
UserRegisterationSerializer, | ||
) | ||
|
||
# @api_view(["GET", "POST"]) | ||
# def hello_world(request): | ||
# if request.method == "POST": | ||
# return Response({"message": "Got some data!", "data": request.data}) | ||
# return {"message": "Hello, world!"} | ||
# User | ||
User = get_user_model() | ||
|
||
|
||
# !UserRegisterationAPIView | ||
class UserRegisterationAPIView(GenericAPIView): | ||
""" | ||
An endpoint for the client to create a new User. | ||
""" | ||
|
||
permission_classes = (AllowAny,) | ||
serializer_class = UserRegisterationSerializer | ||
|
||
def post(self, request, *args, **kwargs): | ||
serializer = self.get_serializer(data=request.data) | ||
serializer.is_valid(raise_exception=True) | ||
user = serializer.save() | ||
token = RefreshToken.for_user(user) | ||
data = serializer.data | ||
data["tokens"] = {"refresh": str(token), "access": str(token.access_token)} | ||
return Response(data, status=status.HTTP_201_CREATED) | ||
|
||
|
||
# !UserLoginAPIView | ||
class UserLoginAPIView(GenericAPIView): | ||
""" | ||
An endpoint to authenticate existing users using their email and password. | ||
""" | ||
|
||
permission_classes = (AllowAny,) | ||
serializer_class = UserLoginSerializer | ||
|
||
def post(self, request, *args, **kwargs): | ||
serializer = self.get_serializer(data=request.data) | ||
serializer.is_valid(raise_exception=True) | ||
user = serializer.validated_data | ||
serializer = CustomUserSerializer(user) | ||
token = RefreshToken.for_user(user) | ||
data = serializer.data | ||
data["tokens"] = {"refresh": str(token), "access": str(token.access_token)} | ||
return Response(data, status=status.HTTP_200_OK) | ||
|
||
|
||
# !UserLogoutAPIView | ||
class UserLogoutAPIView(GenericAPIView): | ||
""" | ||
An endpoint to logout users. | ||
""" | ||
|
||
permission_classes = (IsAuthenticated,) | ||
|
||
def post(self, request, *args, **kwargs): | ||
try: | ||
refresh_token = request.data["refresh"] | ||
token = RefreshToken(refresh_token) | ||
token.blacklist() | ||
return Response(status=status.HTTP_205_RESET_CONTENT) | ||
except Exception: | ||
return Response(status=status.HTTP_400_BAD_REQUEST) | ||
|
||
|
||
# !UserAPIView | ||
class UserAPIView(RetrieveUpdateAPIView): | ||
""" | ||
Get, Update user information | ||
""" | ||
|
||
permission_classes = (IsAuthenticated,) | ||
serializer_class = CustomUserSerializer | ||
|
||
def get_object(self): | ||
return self.request.user |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.