Skip to content

Commit

Permalink
fix: missing provider on user loader
Browse files Browse the repository at this point in the history
  • Loading branch information
richardnguyen99 committed Dec 15, 2023
1 parent df9be5f commit 78128b2
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions cursus/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from .apis import api_bp as api_bp_v1
from .views import view_bp, oauth_bp
from .util.extensions import db, migrate, ma, login_manager, assets, cache
from .models import User, ActiveToken
from .models import User, ActiveToken, Account
from .util import generate_content_hash


Expand Down Expand Up @@ -119,20 +119,28 @@ def create_app() -> Flask:

@login_manager.user_loader
def load_user(id):
"""Load a user from the database"""

user_with_token = (
db.session.query(ActiveToken.token, User)
db.session.query(ActiveToken.token, Account.provider, User)
.select_from(User)
.outerjoin(ActiveToken, User.id == ActiveToken.user_id)
.outerjoin(Account, Account.userId == User.id)
.filter(User.id == id)
.first()
)

# In case the user id is sent but it's no longer in the database
if not user_with_token:
return None

# The above query returns a tuple of an API token and a User object
# However, Flask-Login expects a User object, so we have to set the
# active token manually
user_with_token[1].active_token = user_with_token[0]
user_with_token[2].provider = user_with_token[1]
user_with_token[2].active_token = user_with_token[0]

return user_with_token[1]
return user_with_token[2]

@login_manager.unauthorized_handler
def handle_needs_login():
Expand Down

0 comments on commit 78128b2

Please sign in to comment.