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

Adding user attributes after successful login? #796

Closed
esranonff opened this issue May 7, 2023 · 3 comments
Closed

Adding user attributes after successful login? #796

esranonff opened this issue May 7, 2023 · 3 comments
Labels

Comments

@esranonff
Copy link

Hello!

When a user logs in successfully, I want to query the database for various relationship data and add it to the current_user object.

I thought the following would work, but that turns out to be for the login view rather than post-successful login, so I'm wondering if there's another view that I should be decorating because I can't see one that would work from the docs?

# Add fields to the user when they log in
@app.security.login_context_processor
def add_user_attributes():
        current_user.play_partner_count = PlayPartner.query.filter_by(initiator_user_id=current_user.id).count()

The error I am getting is:

AttributeError: 'AnonymousUser' object has no attribute 'id'

and I get this before I can even display the log in form.

if I update the code as follows:

# Add fields to the user when they log in
@app.security.login_context_processor
def add_user_attributes():
        if current_user.is_anonymous is False:
                current_user.play_partner_count = PlayPartner.query.filter_by(initiator_user_id=current_user.id).count()

Then I get

TypeError: 'NoneType' object is not iterable

Presumably there's a way to do this without having to override the entire login function?

@jwag956
Copy link
Collaborator

jwag956 commented May 8, 2023

I am not quite certain what you are trying to accomplish. If you simply want to provide information to your front-end based on the current user - why not create an endpoint that your front-end calls after successful login?

Note that 'currrent_user' isn't a database object nor does it persist after the request is done.

@esranonff
Copy link
Author

Note that 'currrent_user' isn't a database object nor does it persist after the request is done.

Ah, then that's me holding it upside down and doing it wrong 🤣

Thanks, I'll close this and find another approach.

@jwag956
Copy link
Collaborator

jwag956 commented May 8, 2023

And to be more precise/accurate - current_user is a proxy to the User model which is a DB object - but one you have to manage - if you want to update a field in your user model (say if you have added fields to it) you can start with current_user.my_added_field = "something great" - then you need to 'commit' it - for SQLAlchemy style databases - you need to do a userdatastore.put().

A context manager is not the correct place for this - if you need to update the User model as part of successful login - you should subscribe to the 'user_authenticated' signal (see docs for info about signals).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants