Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
to-sta committed Jun 9, 2024
1 parent 585f7fd commit b2b2d8d
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 23 deletions.
1 change: 1 addition & 0 deletions backend/authentication/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class Meta:
description = factory.Faker("text", max_nb_chars=500)
verified = factory.Faker("boolean")
verification_method = factory.Faker("word")
verification_code = factory.Faker("uuid4")
email = factory.Faker("email")
social_links = factory.List([factory.Faker("user_name") for _ in range(3)])
is_private = factory.Faker("boolean")
Expand Down
2 changes: 1 addition & 1 deletion backend/authentication/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ class UserModel(AbstractUser, PermissionsMixin):
icon_url = models.ForeignKey(
"content.Image", on_delete=models.SET_NULL, blank=True, null=True
)
verifictaion_code = models.UUIDField(blank=True, null=True)
email = models.EmailField(blank=True)
code = models.UUIDField(blank=True, null=True)
is_confirmed = models.BooleanField(default=False)
social_links = ArrayField(models.CharField(max_length=255), blank=True, null=True)
is_private = models.BooleanField(default=False)
Expand Down
8 changes: 4 additions & 4 deletions backend/authentication/templates/pwreset_email.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<body>
<p>Hi {{username}},</p>
<p>We have received a request to reset your password. To proceed with the password reset, please click on the link below:</p>
<p><a href={{pwreset_link}}>Reset Password</a></p>
<p>If you did not request a password reset, please ignore this email.</p>
<p><a href="{{pwreset_link}}">Reset Password</a></p>
<p>If you did not request a password reset, then it's safe to ignore this email.</p>
<p>Best regards,</p>
<p>Your activist.org Team</p>
<p>Your activist team</p>
</body>
</html>
</html>
5 changes: 3 additions & 2 deletions backend/authentication/templates/signup_email.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<p>Please click the button below to confirm your email address:</p>
<a href="{{confirmation_link}}" style="display: inline-block; padding: 10px 20px; background-color: #000; color: #fff; text-decoration: none;">Confirm Email</a>

<p>Kind Regards</p>
<p>Best regards,</p>
<p>Your activist team</p>
</body>
</html>
</html>
4 changes: 2 additions & 2 deletions backend/authentication/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def test_signup(client: Client) -> None:
assert response.status_code == 201
assert UserModel.objects.filter(username=username)
# code for Email confirmation is generated and is a UUID
assert isinstance(user.code, UUID)
assert isinstance(user.verifictaion_code, UUID)
assert user.is_confirmed is False
# Confirmation Email was sent
assert len(mail.outbox) == 1
Expand Down Expand Up @@ -148,7 +148,7 @@ def test_signup(client: Client) -> None:
assert UserModel.objects.filter(username=second_username).exists()
assert user.email == ""
assert user.is_confirmed is False
assert user.code is None
assert user.verifictaion_code is None


@pytest.mark.django_db
Expand Down
25 changes: 11 additions & 14 deletions backend/authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@
import uuid
from uuid import UUID


import dotenv
from django.contrib.auth import login
from django.core.mail import send_mail
from django.template.loader import render_to_string
from drf_spectacular.utils import OpenApiParameter, extend_schema

from django.contrib.auth import get_user_model, login
from django.contrib.auth.models import User
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt

from drf_spectacular.utils import OpenApiParameter, extend_schema
from rest_framework import status, viewsets
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.request import Request
Expand Down Expand Up @@ -96,9 +91,9 @@ def post(self, request: Request) -> Response:
user: UserModel = serializer.save()

if user.email != "":
user.code = uuid.uuid4()
user.verifictaion_code = uuid.uuid4()

confirmation_link = f"{FRONTEND_BASE_URL}/confirm/{user.code}"
confirmation_link = f"{FRONTEND_BASE_URL}/confirm/{user.verifictaion_code}"
message = f"Welcome to activist.org, {user.username}!, Please confirm your email address by clicking the link: {confirmation_link}"
html_message = render_to_string(
template_name="signup_email.html",
Expand All @@ -124,11 +119,13 @@ def post(self, request: Request) -> Response:
status=status.HTTP_201_CREATED,
)

@extend_schema(parameters=[OpenApiParameter(name="code", type=str, required=True)])
@extend_schema(
parameters=[OpenApiParameter(name="verifictaion_code", type=str, required=True)]
)
def get(self, request: Request) -> Response:
"""Confirm a user's email address."""
code = request.GET.get("code")
user = UserModel.objects.filter(code=code).first()
verifictaion_code = request.GET.get("verifictaion_code")
user = UserModel.objects.filter(verifictaion_code=verifictaion_code).first()

if user is None:
return Response(
Expand All @@ -137,7 +134,7 @@ def get(self, request: Request) -> Response:
)

user.is_confirmed = True
user.code = ""
user.verifictaion_code = ""
user.save()

return Response(
Expand Down Expand Up @@ -186,9 +183,9 @@ def get(self, request: Request) -> Response:
status=status.HTTP_404_NOT_FOUND,
)

user.code = uuid.uuid4()
user.verifictaion_code = uuid.uuid4()

pwreset_link = f"{FRONTEND_BASE_URL}/pwreset/{user.code}"
pwreset_link = f"{FRONTEND_BASE_URL}/pwreset/{user.verifictaion_code}"
message = "Reset your password at activist.org"
html_message = render_to_string(
template_name="pwreset_email.html",
Expand Down

0 comments on commit b2b2d8d

Please sign in to comment.