Open samul-1 opened 3 years ago
I'm currently using this validator for my username Field that I have found on stack overflow, you can learn how to raise error when the criteria of your validator don't met
from django.utils.translation import gettext_lazy as _
from django.core.exceptions import ValidationError
class UserNameValidator():
""" Validate username to be alpha numeric and ascii only.
Pass this class to ACCOUNT_USERNAME_VALIDATORS
See https://django-allauth.readthedocs.io/en/latest/configuration.html
"""
def __call__(self, username="", *args, **kwargs):
valid = username.isalnum() and username.isascii()
if not valid:
raise ValidationError(_("Only alphanumeric characters are allowed"))
# KLUDGE: instantiate a list of validators
# See https://github.com/pennersr/django-allauth/pull/1648#issuecomment-284223497
customvalidators = [UserNameValidator()]
ACCOUNT_USERNAME_VALIDATORS = 'accounts.validators.customvalidators'
I hope this will help you to prevent your app from crashing, As you may notice if I use other that alphanum chars I get as a response username: ["Only alphanumeric characters are allowed"]
Hello,
I'm getting started with dj-rest-auth and I have an app which allows user to only sign in via google. However, I want users to only be able to access my service with a specific google apps email domain.
I'm using dj-rest-auth in conjunction with django-allauth.
This is what I tried: I created allauth adapters like this:
and in my settings I have
With this solution, if the user tries to sign in with a valid email, everything works, whereas if they use a non-allowed email domain, I get this error in my console:
What I would like to do is for my django app to send a 403 response to the user (or similar) with a message like "You can only access this service with an [at] whatever email address". I'm trying to figure out at which point the dj-rest-auth views call the user adapters and what to do to handle the failure and achieve what I'm after.
I appreciate any guidance.