Skip to content

Commit

Permalink
Upd: Delete by mobile number
Browse files Browse the repository at this point in the history
Signed-off-by: George J Padayatti <[email protected]>
  • Loading branch information
georgepadayatti committed Nov 30, 2023
1 parent 9b384af commit cb5bcaa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 30 deletions.
22 changes: 13 additions & 9 deletions otp_auth/user.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import typing
import string
import typing
from datetime import timedelta
from hashlib import sha256

from django.contrib.auth import get_user_model
from django.contrib.auth.models import AbstractBaseUser
from django.utils import timezone
from django.utils.crypto import get_random_string
from otp_auth.settings import api_settings
from otp_auth.models import OTP
from hashlib import sha256
from datetime import timedelta
from rest_framework.authtoken.models import Token

from otp_auth.models import OTP
from otp_auth.settings import api_settings


def generate_otp(max_length: int):
"""
Expand Down Expand Up @@ -93,12 +95,14 @@ def delete_otp(user: AbstractBaseUser):
"""Delete OTP of the user"""
OTP.objects.filter(user=user).delete()

def delete_user(token: str) -> bool:
def delete_user(mobile_number: str) -> bool:
"""Delete user"""
try:
user = Token.objects.get(key=token).user
user.delete()
return True
user, exists = get_user_by_mobile_number(mobile_number)
if exists:
user.delete()
return True
return False
except Token.DoesNotExist:
return False

31 changes: 10 additions & 21 deletions otp_auth/views.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,15 @@
from rest_framework import status, authentication
from rest_framework import authentication, status
from rest_framework.decorators import api_view, authentication_classes
from rest_framework.response import Response
from otp_auth.serializers import (
RegisterUserSerializer,
LoginUserSerializer,
VerifyOTPSerializer,
ValidateMobileNumberSerializer
)
from twilio.base.exceptions import TwilioRestException
from otp_auth.user import (
get_user_by_mobile_number,
is_user_active,
send_otp_verification_code,
get_otp_by_otp_hash,
is_otp_expired,
issue_token,
delete_token,
delete_otp,
delete_user
)

from otp_auth.serializers import (LoginUserSerializer, RegisterUserSerializer,
ValidateMobileNumberSerializer,
VerifyOTPSerializer)
from otp_auth.user import (delete_otp, delete_token, delete_user,
get_otp_by_otp_hash, get_user_by_mobile_number,
is_otp_expired, is_user_active, issue_token,
send_otp_verification_code)


@api_view(['POST'])
Expand Down Expand Up @@ -133,10 +124,8 @@ def validate_mobile_number(request):
return Response(response_data, status=status.HTTP_200_OK)

@api_view(['POST'])
@authentication_classes([authentication.TokenAuthentication])
def delete_user_account(request):
token = request.headers.get("Authorization").split("Bearer Token ")[1]
is_deleted = delete_user(token)
is_deleted = delete_user(request.data.get("mobile_number"))
if is_deleted:
return Response(status=status.HTTP_204_NO_CONTENT)
else:
Expand Down

0 comments on commit cb5bcaa

Please sign in to comment.