Skip to content

Commit fcf0f77

Browse files
committed
improve verification email
1 parent 3324cb9 commit fcf0f77

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

web/authapi/tasks.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
@shared_task
11-
def send_verification_email(user_id):
11+
def send_verification_email(user_id, ip_address=None):
1212
"""Send email verification link to user."""
1313
from .models import EmailVerificationToken
1414

@@ -25,18 +25,27 @@ def send_verification_email(user_id):
2525
verify_url = f"{base_url}/verify-email/{token_obj.token}"
2626

2727
subject = "Verify your email address"
28-
message = f"""Hi,
2928

30-
Please verify your email address by clicking the link below:
29+
ip_line = ""
30+
if ip_address:
31+
ip_line = f"\nThe request for this verification originated from IP address {ip_address}\n"
32+
33+
message = f"""Verify your email address
34+
35+
You need to verify your email address to continue using your account. Click the link below to verify your email address:
3136
3237
{verify_url}
3338
3439
This link will expire in 7 days.
40+
{ip_line}
41+
In case you did not sign up for this account and are seeing this email, please follow the instructions below:
3542
36-
If you didn't create an account, you can ignore this email.
43+
- Reset your password: {base_url}/forgot-password
44+
- Check if any changes were made to your account settings. If yes, revert them immediately.
45+
- If you are unable to access your account, please contact us.
3746
3847
Thanks,
39-
The Team
48+
Thatcher
4049
"""
4150

4251
send_mail(

web/authapi/views.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@
88
from rest_framework.views import APIView
99

1010
from .models import EmailVerificationToken, PasswordResetToken
11+
12+
13+
def get_client_ip(request):
14+
"""Extract client IP address from request, handling proxy headers."""
15+
x_forwarded_for = request.META.get("HTTP_X_FORWARDED_FOR")
16+
if x_forwarded_for:
17+
return x_forwarded_for.split(",")[0].strip()
18+
return request.META.get("REMOTE_ADDR")
19+
20+
1121
from .serializers import (
1222
ChangePasswordSerializer,
1323
DeleteAccountSerializer,
@@ -34,8 +44,8 @@ def post(self, request):
3444
password=serializer.validated_data["password"],
3545
)
3646

37-
# Send verification email asynchronously
38-
send_verification_email.delay(user.id)
47+
client_ip = get_client_ip(request)
48+
send_verification_email.delay(user.id, ip_address=client_ip)
3949

4050
# Log the user in
4151
login(request, user, backend="django.contrib.auth.backends.ModelBackend")
@@ -243,7 +253,8 @@ def post(self, request):
243253
status=status.HTTP_400_BAD_REQUEST,
244254
)
245255

246-
send_verification_email.delay(user.id)
256+
client_ip = get_client_ip(request)
257+
send_verification_email.delay(user.id, ip_address=client_ip)
247258

248259
return Response({"message": "Verification email sent."})
249260

0 commit comments

Comments
 (0)