refactor: combine sign in and sign up endpoint to a single endpoint (#263)

This commit is contained in:
pablohashescobar 2023-02-14 01:14:56 +05:30 committed by GitHub
parent 0477db69a0
commit af1d49bbf5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 102 deletions

View File

@ -5,7 +5,6 @@ from django.urls import path
from plane.api.views import ( from plane.api.views import (
# Authentication # Authentication
SignUpEndpoint,
SignInEndpoint, SignInEndpoint,
SignOutEndpoint, SignOutEndpoint,
MagicSignInEndpoint, MagicSignInEndpoint,
@ -95,7 +94,6 @@ urlpatterns = [
path("social-auth/", OauthEndpoint.as_view(), name="oauth"), path("social-auth/", OauthEndpoint.as_view(), name="oauth"),
# Auth # Auth
path("sign-in/", SignInEndpoint.as_view(), name="sign-in"), path("sign-in/", SignInEndpoint.as_view(), name="sign-in"),
path("sign-up/", SignUpEndpoint.as_view(), name="sign-up"),
path("sign-out/", SignOutEndpoint.as_view(), name="sign-out"), path("sign-out/", SignOutEndpoint.as_view(), name="sign-out"),
# Magic Sign In/Up # Magic Sign In/Up
path( path(

View File

@ -64,7 +64,6 @@ from .auth_extended import (
from .authentication import ( from .authentication import (
SignUpEndpoint,
SignInEndpoint, SignInEndpoint,
SignOutEndpoint, SignOutEndpoint,
MagicSignInEndpoint, MagicSignInEndpoint,

View File

@ -84,7 +84,7 @@ class ForgotPasswordEndpoint(BaseAPIView):
) )
return Response( return Response(
{"messgae": "Check your email to reset your password"}, {"message": "Check your email to reset your password"},
status=status.HTTP_200_OK, status=status.HTTP_200_OK,
) )
return Response( return Response(

View File

@ -35,7 +35,7 @@ def get_tokens_for_user(user):
) )
class SignUpEndpoint(BaseAPIView): class SignInEndpoint(BaseAPIView):
permission_classes = (AllowAny,) permission_classes = (AllowAny,)
def post(self, request): def post(self, request):
@ -62,114 +62,67 @@ class SignUpEndpoint(BaseAPIView):
user = User.objects.filter(email=email).first() user = User.objects.filter(email=email).first()
if user is not None: # Sign up Process
return Response( if user is None:
{"error": "Email ID is already taken"}, user = User.objects.create(email=email, username=uuid.uuid4().hex)
status=status.HTTP_400_BAD_REQUEST, user.set_password(password)
)
user = User.objects.create(email=email) # settings last actives for the user
user.set_password(password) user.last_active = timezone.now()
user.last_login_time = timezone.now()
user.last_login_ip = request.META.get("REMOTE_ADDR")
user.last_login_uagent = request.META.get("HTTP_USER_AGENT")
user.token_updated_at = timezone.now()
user.save()
# settings last actives for the user serialized_user = UserSerializer(user).data
user.last_active = timezone.now()
user.last_login_time = timezone.now()
user.last_login_ip = request.META.get("REMOTE_ADDR")
user.last_login_uagent = request.META.get("HTTP_USER_AGENT")
user.token_updated_at = timezone.now()
user.save()
serialized_user = UserSerializer(user).data access_token, refresh_token = get_tokens_for_user(user)
access_token, refresh_token = get_tokens_for_user(user) data = {
"access_token": access_token,
"refresh_token": refresh_token,
"user": serialized_user,
}
data = { return Response(data, status=status.HTTP_200_OK)
"access_token": access_token, # Sign in Process
"refresh_token": refresh_token, else:
"user": serialized_user, if not user.check_password(password):
} return Response(
{
"error": "Sorry, we could not find a user with the provided credentials. Please try again."
},
status=status.HTTP_403_FORBIDDEN,
)
if not user.is_active:
return Response(
{
"error": "Your account has been deactivated. Please contact your site administrator."
},
status=status.HTTP_403_FORBIDDEN,
)
return Response(data, status=status.HTTP_200_OK) serialized_user = UserSerializer(user).data
except Exception as e: # settings last active for the user
capture_exception(e) user.last_active = timezone.now()
return Response( user.last_login_time = timezone.now()
{ user.last_login_ip = request.META.get("REMOTE_ADDR")
"error": "Something went wrong. Please try again later or contact the support team." user.last_login_uagent = request.META.get("HTTP_USER_AGENT")
}, user.token_updated_at = timezone.now()
status=status.HTTP_400_BAD_REQUEST, user.save()
)
access_token, refresh_token = get_tokens_for_user(user)
class SignInEndpoint(BaseAPIView): data = {
permission_classes = (AllowAny,) "access_token": access_token,
"refresh_token": refresh_token,
"user": serialized_user,
}
def post(self, request): return Response(data, status=status.HTTP_200_OK)
try:
email = request.data.get("email", False)
password = request.data.get("password", False)
## Raise exception if any of the above are missing
if not email or not password:
return Response(
{"error": "Both email and password are required"},
status=status.HTTP_400_BAD_REQUEST,
)
email = email.strip().lower()
try:
validate_email(email)
except ValidationError as e:
return Response(
{"error": "Please provide a valid email address."},
status=status.HTTP_400_BAD_REQUEST,
)
user = User.objects.get(email=email)
if not user.check_password(password):
return Response(
{
"error": "Sorry, we could not find a user with the provided credentials. Please try again."
},
status=status.HTTP_403_FORBIDDEN,
)
if not user.is_active:
return Response(
{
"error": "Your account has been deactivated. Please contact your site administrator."
},
status=status.HTTP_403_FORBIDDEN,
)
serialized_user = UserSerializer(user).data
# settings last active for the user
user.last_active = timezone.now()
user.last_login_time = timezone.now()
user.last_login_ip = request.META.get("REMOTE_ADDR")
user.last_login_uagent = request.META.get("HTTP_USER_AGENT")
user.token_updated_at = timezone.now()
user.save()
access_token, refresh_token = get_tokens_for_user(user)
data = {
"access_token": access_token,
"refresh_token": refresh_token,
"user": serialized_user,
}
return Response(data, status=status.HTTP_200_OK)
except User.DoesNotExist:
return Response(
{
"error": "Sorry, we could not find a user with the provided credentials. Please try again."
},
status=status.HTTP_403_FORBIDDEN,
)
except Exception as e: except Exception as e:
capture_exception(e) capture_exception(e)
return Response( return Response(