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

Problem in getting current_user #1281

Closed
LidorPrototype opened this issue Aug 30, 2023 · 1 comment
Closed

Problem in getting current_user #1281

LidorPrototype opened this issue Aug 30, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@LidorPrototype
Copy link

LidorPrototype commented Aug 30, 2023

Describe the bug

When trying to get current user I'm getting the error

File ".\main.py", line 788, in tester
    current_user = Depends(FastAPIUsers.current_user())
AttributeError: type object 'FastAPIUsers' has no attribute 'current_user'

The code is:

from fastapi_users import FastAPIUsers
current_user = Depends(FastAPIUsers.current_user())

Expected behavior

I expected to get the current user that sent the request to my FastAPI in any format that is readable

Configuration

  • Python version : 3.8.2
  • FastAPI version : 0.103.0
  • FastAPI Users version : 12.1.2

Additional context

I built the API with FastAPI and I'm trying to find out which is the user that sent the request to me

@LidorPrototype LidorPrototype added the bug Something isn't working label Aug 30, 2023
@nurettn
Copy link

nurettn commented Aug 30, 2023

@LidorPrototype hi,

In the documentation it's saying:

FastAPI Users provides a dependency callable to easily inject authenticated user in your routes. They are available from your FastAPIUsers instance.

So you need to instantiate a FastAPIUsers object and derive current_users from this. Like this:

from fastapi_users import FastAPIUsers
...
fastapi_users = FastAPIUsers[User, PydanticObjectId](get_user_manager, [auth_backend])
current_active_user = fastapi_users.current_user(active=True)

after you can use this variable like this:

async def get_database_meta(database_id: PydanticObjectId, user: User = Depends(current_active_user)):
    return await DatabaseMetadata.find_one(DatabaseMetadata.user == user.id, DatabaseMetadata.id == database_id)

I believe that the examples in the documentation are confusing for users because the variable fastapi_users has the same name as the package.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants