Does Flask-Login support roles?

Viewed 3347

If not, are there any projects that have added this feature to Flask-Login? Otherwise, it appears to be a bit daunting to migrate from Flask-Login to Flask-User. Otherwise, is there any sort of direction out there for migrating from Flask-Login to Flask-User?

1 Answers

Again, answering my own question here for anyone else curious how you can add the feature of handling multiple roles while still using Flask-Login. I created the below decorator which just checks the current_user.role to see if it is "Admin". You should also check the same thing when letting the user log in, depending on if they are logging in to the admin or the user panel.

from functools import wraps

def admin_required(f):
    @wraps(f)
    def wrap(*args, **kwargs):
        if current_user.role == "Admin":
            return f(*args, **kwargs)
        else:
            flash("You need to be an admin to view this page.")
            return redirect(url_for('index'))

    return wrap
Related