5

I'm using django 1.6 with allauth. I've just enabled the email verification stuff and am looking for the best way to identify if a user has a verified email or not. One interesting thing I encountered and wanted to ask about: I noticed that a user can have several email addresses. Why is it so? this makes the above test a bit more complicated since you have to ask "does the user have at least one verified email address?"

sivanr
  • 477
  • 5
  • 10
  • First half is duplicate of [How to tell if user's email address has been verified using Django, allauth, rest-auth and a custom user - Stack Overflow](https://stackoverflow.com/questions/54467321/how-to-tell-if-users-email-address-has-been-verified-using-django-allauth-res#54467557) (although the latter is newer, it appears to be more popular.). – user202729 Jul 14 '22 at 12:44

1 Answers1

14

allauth offers a decorator for this:

from allauth.account.decorators import verified_email_required

@verified_email_required
def verified_users_only_view(request):
    ...

Alternatively, you can use this to check things yourself:

from allauth.account.models import EmailAddress

if EmailAddress.objects.filter(user=request.user, verified=True).exists():
    ...

The above works regardless how many email addresses the user has setup...

Craig Anderson
  • 754
  • 1
  • 11
  • 18
pennersr
  • 6,306
  • 1
  • 30
  • 37