How can I implement email verification in Django
Completely stumped! I'm using the console as my email backend. I end up with False
in token_generator.check_token
as a result "Invalid or expired token." is displayed in my homepage when I navigate to say "http://localhost:8000/user/verify-email/?token=cgegv3-ec1fe9eb2cebc34e240791d72fb10d7d&email=test16@example.com"
Here's my code
from django.contrib.auth.tokens import PasswordResetTokenGenerator
class CustomPasswordResetTokenGenerator(PasswordResetTokenGenerator):
pass
# Define a single instance of the token generator
token_generator = CustomPasswordResetTokenGenerator()
def verify_email(request):
email = request.GET.get("email")
token = request.GET.get("token")
try:
user = CustomUser.objects.get(email=email)
except CustomUser.DoesNotExist:
messages.error(request, "Invalid verification link.")
return redirect("home")
if token_generator.check_token(user, token):
user.is_active = True
user.save()
messages.success(request, "Your email has been verified!")
return redirect("sign_in")
else:
messages.error(request, "Invalid or expired token.")
return redirect("home")
from django.core.mail import send_mail
from django.urls import reverse
from user_management.utils import token_generator
def send_verification_email(user, request):
token = token_generator.make_token(user)
verification_url = request.build_absolute_uri(
reverse("verify_email") + f"?token={token}&email={user.email}"
)
send_mail(
"Verify your email",
f"Click the link to verify your email: {verification_url}",
"no-reply@example.com",
[user.email],
fail_silently=False,
)
The best way for that is to using Django Allauth, You can do anything with that secure and easy, For sure Allauth have many other options for you.