Skip to content

Commit

Permalink
store user on g instead of app_ctx_stack.top
Browse files Browse the repository at this point in the history
Fixes compatibility with Flask 2.2
  • Loading branch information
davidism committed Jul 25, 2022
1 parent f8a2c84 commit 359fb00
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Version 0.6.2

Unreleased

- Fix compatibility with Werkzeug 2.2 and Flask 2.2. #691
- Revert change to `expand_login_view` that attempted to preserve a
dynamic subdomain value. Such values should be handled using
`app.url_value_preprocessor` and `app.url_defaults`. #691
Expand Down
8 changes: 5 additions & 3 deletions src/flask_login/login_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
from datetime import datetime
from datetime import timedelta

from flask import _request_ctx_stack
from flask import abort
from flask import current_app
from flask import flash
from flask import g
from flask import has_app_context
from flask import redirect
from flask import request
Expand Down Expand Up @@ -328,8 +328,10 @@ def header_loader(self, callback):
def _update_request_context_with_user(self, user=None):
"""Store the given user as ctx.user."""

ctx = _request_ctx_stack.top
ctx.user = self.anonymous_user() if user is None else user
if user is None:
user = self.anonymous_user()

g._login_user = user

def _load_user(self):
"""Loads user from session or remember_me cookie as applicable"""
Expand Down
11 changes: 7 additions & 4 deletions src/flask_login/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from urllib.parse import urlparse
from urllib.parse import urlunparse

from flask import _request_ctx_stack
from flask import current_app
from flask import g
from flask import has_request_context
from flask import request
from flask import session
Expand Down Expand Up @@ -367,10 +367,13 @@ def set_login_view(login_view, blueprint=None):


def _get_user():
if has_request_context() and not hasattr(_request_ctx_stack.top, "user"):
current_app.login_manager._load_user()
if has_request_context():
if "_login_user" not in g:
current_app.login_manager._load_user()

return getattr(_request_ctx_stack.top, "user", None)
return g._login_user

return None


def _cookie_digest(payload, key=None):
Expand Down

0 comments on commit 359fb00

Please sign in to comment.