Skip to content

Instantly share code, notes, and snippets.

@iMerica
Last active July 27, 2023 17:20
Show Gist options
  • Save iMerica/a6a7efd80d49d6de82c7928140676957 to your computer and use it in GitHub Desktop.
Save iMerica/a6a7efd80d49d6de82c7928140676957 to your computer and use it in GitHub Desktop.
Email verification in Django Rest Framework, Django All-Auth, Django Rest-Auth. Suitable for Single Page Applications
urlpatterns = [
url(r'^rest-auth/registration/account-confirm-email/(?P<key>[-:\w]+)/$', ConfirmEmailView.as_view(), name='account_confirm_email'),
]
from rest_framework.exceptions import NotFound
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.permissions import AllowAny
from allauth.account.models import EmailConfirmation, EmailConfirmationHMAC
class ConfirmEmailView(APIView):
permission_classes = [AllowAny]
def get(self, *args, **kwargs):
self.object = confirmation = self.get_object()
confirmation.confirm(self.request)
# A React Router Route will handle the failure scenario
return HttpResponseRedirect('/login/success/')
def get_object(self, queryset=None):
key = self.kwargs['key']
email_confirmation = EmailConfirmationHMAC.from_key(key)
if not email_confirmation:
if queryset is None:
queryset = self.get_queryset()
try:
email_confirmation = queryset.get(key=key.lower())
except EmailConfirmation.DoesNotExist:
# A React Router Route will handle the failure scenario
return HttpResponseRedirect('/login/failure/')
return email_confirmation
def get_queryset(self):
qs = EmailConfirmation.objects.all_valid()
qs = qs.select_related("email_address__user")
return qs
@hussu010
Copy link

Hi @iMerica if by any chance you're reading this comment, can you please help me fix the error that I'm getting when I set up this part
'Response' object has no attribute 'confirm'

@atikmahbub
Copy link

working but user can login without clicking the link , i need to prevent login user until link clicked.can you help please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment