I'm trying to restrict registration in my Flask application built with Flask-Security to email addresses from specific domains.
My additional inclination was to override the register_user
view from Flask Security, but their docs made it seem like extending the form was the preferred route.
In the following code, I attempt to add a custom validator to email
, a field that is already declared within the Flask Security's RegisterForm
. The additional validation function is never called. I have tried declaring the variable outside of the class and calling it from within the EmailField
, but that doesn't seem to help either.
This is all within app.py.
class RestrictedRegisterForm(RegisterForm):
email = EmailField(
'Email', validators=[DataRequired()]
)
def validate_email(form, field):
domain_list = ['foo.com', 'bar.org']
email_domain = field.data.split("@")[1]
if email_domain not in domain_list:
raise ValidationError('Email address must be of an authorized domain')
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore, register_form=RestrictedRegisterForm)