3

Is there some convenient way to override register view in Flask-Security? I'm pretty happy with the built-in view for registration, but I need it to be accessible only after authentication and by certain roles.

I tried to override it like this (with SECURITY_REGISTER_URL='/register/') but that just takes the built-in function and mine is completely ignored:

@app.route('/register/')
@login_required
@roles_required('superuser')
def register():
    """View function which handles a registration request."""
    ...

So the question is - do I have to write my own register view from peak and disable the Flask-Security registration?

I thought also about checking the user login/role in register_user.html template and possibly redirecting back to home page, but I'm not sure if that's the correct way to achieve this...

Cœur
  • 37,241
  • 25
  • 195
  • 267
drahoja9
  • 81
  • 7

1 Answers1

3

After several hours I finally managed to get it working. I forgot to specify methods, while registering the route @app.route('/register/', methods=['GET', 'POST']). If you disable the original Flask-Security's register view (SECURITY_REGISTERABLE = False), it works like a charm!

Also, when you want to override some view from Flask-Security, that can't be disabled so easily, like login, you have to do the same thing as mentioned above, but also register your view/blueprint first!

drahoja9
  • 81
  • 7
  • After way too much time fighting with Flask-Security on this, I finally found this answer and finally got user registration working in my project. Thank you! Strangely, my `'/register'` route used to work and then somehow Flask-Security's took over. Just disabling it was the right thing to do: I never wanted Flask-Security's view to run since I had to set up a custom form object and do extra work after registration. Of course, I then had to change my `url_for` calls. – Nick K9 Apr 26 '19 at 10:24