简体   繁体   中英

I code two user_loader on different place but only first one works in flask_login?

My first user_loader in admin/login.py and my second user_loader in user/login.py but user_loader in admin/login was called when I use user/login.py; error like this: enter image description here

Maybe some problems happened in other place but the value of is_authenticated was True.I guess I used user_load with wrong way. Help! Thanks in advance.

I used 2 LoginManagers in flask,one used as user and another as administrater , but I found only one works, so I rewrite login_required refer to this anwser

my User model like this.

class User(UserMixin):
def __init__(self, username, role):
    self.username = username
    self.id = self.get_id()
    self.urole = role

@property
def password(self):
    raise AttributeError('password is not a readable attribute')

@password.setter
def password(self, password):
    self.password_hash = generate_password_hash(password)
    admin_collection.insert_one({'username':self.username,'password':self.password_hash,'id':self.id})

def verify_password(self, password):
    password_hash = self.get_password_hash()
    if password_hash is None:
        return False
    return check_password_hash(password_hash, password)

def get_password_hash(self):
    pswd = admin_collection.find_one({'username':self.username})
    return pswd['password'] if pswd else None

def get_id(self):
    find_id = admin_collection.find_one({'username':self.username})
    return find_id['id']

def get_role(self):
    return self.urole

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM