Flask-Security override register view

2019-07-10 04:04发布

问题:

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...

回答1:

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!