How can I restrict Flask-User sign ups to users with emails on a specific domain?

1.2k Views Asked by At

I'm building a Python web application using Flask along with Flask-User. I want to be able to restrict user registration to users that have an email on a specific domain (i.e. whoever@example.com)

Is this possible?

2

There are 2 best solutions below

1
On BEST ANSWER

You could validate the E-Mail in the RegisterForm, so only users with E-Mail ending on example.com will be allowed to register. If you are using Flask-WTF you can check the documentation of WTForms for custom validators.

0
On

You could do it validate it before sending the form, like this:

DOMAINS_NOT_ALLOWED = ['yahoo.com', 'baidu.ch', 'example.com']
email_domain = request.form['email'].split('@')[-1]

if email_domain in DOMAINS_NOT_ALLOWED:
    return "You're not allowed to register from this email provider."