Last active
July 27, 2023 17:20
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
urlpatterns = [ | |
url(r'^rest-auth/registration/account-confirm-email/(?P<key>[-:\w]+)/$', ConfirmEmailView.as_view(), name='account_confirm_email'), | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
working but user can login without clicking the link , i need to prevent login user until link clicked.can you help please?