fix: auth register api fixed
This commit is contained in:
@@ -6,4 +6,5 @@ CELERY_ACCEPT_CONTENT = ['json']
|
||||
CELERY_TASK_SERIALIZER = 'json'
|
||||
CELERY_TIMEZONE = settings.TIME_ZONE
|
||||
|
||||
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
|
||||
if not env('DEBUG'):
|
||||
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
|
||||
|
||||
12
core/apps/accounts/cache/user.py
vendored
12
core/apps/accounts/cache/user.py
vendored
@@ -7,12 +7,15 @@ from config.env import env
|
||||
r = redis.StrictRedis.from_url(env.str('REDIS_URL'))
|
||||
|
||||
|
||||
def cache_user_credentials(phone_number, password, time):
|
||||
def cache_user_credentials(phone_number, password, first_name, last_name, email, time):
|
||||
key = f"user_credentials:{phone_number}"
|
||||
|
||||
r.hmset(key, {
|
||||
"phone": phone_number,
|
||||
"password": password
|
||||
"password": password,
|
||||
"first_name": first_name,
|
||||
"last_name": last_name,
|
||||
"email": email
|
||||
})
|
||||
|
||||
r.expire(key, time)
|
||||
@@ -27,5 +30,8 @@ def get_user_creadentials(phone_number):
|
||||
|
||||
return {
|
||||
"phone": data.get(b"phone").decode() if data.get(b"phone") else None,
|
||||
"password": data.get(b"password").decode() if data.get(b"password") else None
|
||||
"password": data.get(b"password").decode() if data.get(b"password") else None,
|
||||
"first_name": data.get(b"first_name").decode() if data.get(b'first_name') else None,
|
||||
"last_name": data.get(b"last_name").decode() if data.get(b'last_name') else None,
|
||||
"email": data.get(b"email").decode() if data.get(b'email') else None,
|
||||
}
|
||||
@@ -12,7 +12,7 @@ class VerificationCode(BaseModel):
|
||||
expiration_time = models.TimeField(null=True, blank=True)
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.user.phone} - {self.code}'
|
||||
return f'{self.phone} - {self.code}'
|
||||
|
||||
class Meta:
|
||||
verbose_name = 'Verification Code'
|
||||
|
||||
@@ -28,12 +28,20 @@ class LoginSerializer(serializers.Serializer):
|
||||
class RegisterSerializer(serializers.Serializer):
|
||||
phone = serializers.CharField()
|
||||
password = serializers.CharField()
|
||||
first_name = serializers.CharField()
|
||||
last_name = serializers.CharField()
|
||||
email = serializers.EmailField()
|
||||
|
||||
def validate_phone(self, value):
|
||||
if User.objects.filter(phone=value).exists():
|
||||
raise serializers.ValidationError("User exists")
|
||||
raise serializers.ValidationError("User exists with this phone")
|
||||
return value
|
||||
|
||||
|
||||
def validate_email(self, value):
|
||||
if User.objects.filter(email=value).exists():
|
||||
raise serializers.ValidationError("User exists with this email")
|
||||
return value
|
||||
|
||||
|
||||
class ConfirmUserSerializer(serializers.Serializer):
|
||||
phone = serializers.CharField()
|
||||
@@ -52,24 +60,4 @@ class ConfirmUserSerializer(serializers.Serializer):
|
||||
|
||||
|
||||
class ChoiseRoleSerializer(serializers.Serializer):
|
||||
role = serializers.ChoiceField(choices=ROLE_CHOICES)
|
||||
|
||||
|
||||
class CompliteUserProfileSerializer(serializers.Serializer):
|
||||
first_name = serializers.CharField()
|
||||
last_name = serializers.CharField()
|
||||
email = serializers.EmailField()
|
||||
|
||||
def validate(self, data):
|
||||
user = User.objects.filter(email=data.get('email')).first()
|
||||
if user:
|
||||
raise serializers.ValidationError({'detail': "User with this email already exists"})
|
||||
return data
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
with transaction.atomic():
|
||||
instance.first_name = validated_data.get('first_name')
|
||||
instance.last_name = validated_data.get('last_name')
|
||||
instance.email = validated_data.get('email')
|
||||
instance.save()
|
||||
return instance
|
||||
role = serializers.ChoiceField(choices=ROLE_CHOICES)
|
||||
@@ -1,6 +1,6 @@
|
||||
from django.urls import path, include
|
||||
|
||||
from core.apps.accounts.views.auth import LoginApiView, RegisterApiView, ConfirUserApiView, ChoiceUserRoleApiView, CompliteUserProfileApiView
|
||||
from core.apps.accounts.views.auth import LoginApiView, RegisterApiView, ConfirUserApiView, ChoiceUserRoleApiView
|
||||
|
||||
urlpatterns = [
|
||||
path('auth/', include(
|
||||
@@ -9,7 +9,6 @@ urlpatterns = [
|
||||
path('register/', RegisterApiView.as_view(), name='register'),
|
||||
path('confirm_user/', ConfirUserApiView.as_view(), name='confirm-user'),
|
||||
path('choise_user_role/', ChoiceUserRoleApiView.as_view(), name='choise-user-role'),
|
||||
path('complite_user_profile/', CompliteUserProfileApiView.as_view(), name='complite-user-profile'),
|
||||
]
|
||||
))
|
||||
]
|
||||
@@ -37,7 +37,10 @@ class RegisterApiView(generics.GenericAPIView):
|
||||
serializer = self.serializer_class(data=request.data)
|
||||
if serializer.is_valid():
|
||||
data = serializer.validated_data
|
||||
cache_user_credentials(data['phone'], data['password'], 300)
|
||||
cache_user_credentials(
|
||||
data['phone'], data['password'], data['first_name'],
|
||||
data['last_name'], data['email'], 300
|
||||
)
|
||||
user_tasks.create_and_send_sms_code.delay(data['phone'])
|
||||
return success_message("code is send", 200)
|
||||
return error_message(serializer.errors, 400)
|
||||
@@ -56,7 +59,10 @@ class ConfirUserApiView(generics.GenericAPIView):
|
||||
data = get_user_creadentials(phone)
|
||||
if not data:
|
||||
return error_message("Not found", 404)
|
||||
user = User.objects.create_user(phone=data['phone'])
|
||||
user = User.objects.create_user(
|
||||
phone=data['phone'], first_name=data['first_name'],
|
||||
last_name=data['last_name'], email=data['email']
|
||||
)
|
||||
user.set_password(data['password'])
|
||||
user.save()
|
||||
confirmation.is_verify = True
|
||||
@@ -82,18 +88,3 @@ class ChoiceUserRoleApiView(generics.GenericAPIView):
|
||||
user.save()
|
||||
return success_message('role choices', 200)
|
||||
return error_message(serializer.errors, 400)
|
||||
|
||||
|
||||
class CompliteUserProfileApiView(generics.GenericAPIView):
|
||||
serializer_class = auth_serializer.CompliteUserProfileSerializer
|
||||
queryset = User.objects.all()
|
||||
|
||||
def put(self, request):
|
||||
user = request.user
|
||||
if user:
|
||||
serializer = self.serializer_class(data=request.data, instance=user)
|
||||
if serializer.is_valid():
|
||||
serializer.save()
|
||||
return success_message("profile complited", 200)
|
||||
return error_message(serializer.errors, 400)
|
||||
return error_message("User not found", 404)
|
||||
Reference in New Issue
Block a user