Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spurious and random "'flask_login.AnonymousUserMixin object' has no attribute" exceptions #261

Closed
toastwaffle opened this issue Jan 30, 2016 · 1 comment

Comments

@toastwaffle
Copy link

Flaks-Login appears to occasionally fail to do the login_required decorator properly, and allows running the view function when current_user is actually an AnonymousUserMixin object. This results in attribute errors where my code, expecting the user to be a logged in instance of the database model, continues blindly on under that assumption. I don't ever use anonymous users, and unauthenticated users should always be sent to the login page.

The problem doesn't happen consistently (as far as I can identify), and probably only affects about 1 in 1000 requests.

My code is at https://github.com/KebleBall/KebleBall; I'm using Flask-Login v0.3.2.

@alanhamlett
Copy link
Collaborator

alanhamlett commented Aug 2, 2016

If your view needs an authenticated user, decorate it with LoginManager.login_required. If your view can be visited by unauthenticated users, your AnonymousUser class should extend the flask_login.AnonymousUserMixin and stub out methods/properties that are throwing exceptions.

For example:

from myapp import app
from flask_login import LoginManager, AnonymousUserMixin

login_manager = LoginManager()
login_manager.setup_app(current_app)

class AnonymousUser(AnonymousUserMixin):

    @property
    def date_format(self):
        return 'YYYY-MM-DD'

    @property
    def has_premium_features(self):
        return False

login_manager.anonymous_user = AnonymousUser

@github-actions github-actions bot locked and limited conversation to collaborators Apr 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants