6

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?

MBT
  • 21,733
  • 19
  • 84
  • 102
user1995565
  • 165
  • 1
  • 10

1 Answers1

7

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
nbro
  • 15,395
  • 32
  • 113
  • 196
user1995565
  • 165
  • 1
  • 10
  • how to add role to the User Object. i tried hardcoding role at the time User object creation to see if i can fetch it from current_user. but i get - AttributeError: 'User' object has no attribute 'role' – rakesh Aug 19 '20 at 03:59