简体   繁体   中英

flask-login logout AnonymousUserMixin error

I define MembershipUsers class based on UserMixin class of flask-login.

class MembershipUsers(UserMixin):
    applicationid = None
    userid = None
    password = None

    def get_id(self):
        return unicode(self.userid)

And login process below :

def login_membership(userid, password):

    login_result, membership_users = _service.login(userid, password)

    if login_result == LOGIN_RESULT.OK:
        logged_in = login_user(membership_users)

_service.login is a method to login and create MembershipUsers instance. If login_result is OK, call login_user() of flask-login.

And logout below:

def logout_membership():
    logout_user()

Login is ok. But logout_user give the error:

'AttributeError: 'AnonymousUserMixin' object has no attribute 'userid'

why raise this error? Why AnonymousUserMixin?

The error happens because the current user is not logged in. As @IcarianComplex pointed out, the logout method that is logout_membership() doesn't have a @login_required decorator. If you want to do the check internally, then use

if current_user.is_authenticated:
     logout_user()

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