* [ Full Stack FastAPI Template  ](../../../project-generation/)
    * [ External Links and Articles  ](../../../external-links/)
    * [ FastAPI and friends newsletter  ](../../../newsletter/)
  * [ About  ](../../../about/)

About

    * [ Alternatives, Inspiration and Comparisons  ](../../../alternatives/)
    * [ History, Design and Future  ](../../../history-design-future/)
    * [ Benchmarks  ](../../../benchmarks/)
  * [ Help  ](../../../help/)

Help

    * [ Help FastAPI - Get Help  ](../../../help-fastapi/)
    * [ Development - Contributing  ](../../../contributing/)
  * [ Release Notes  ](../../../release-notes/)

Table of contents

  * Create a user model 
  * Create a ` get_current_user ` dependency 
  * Get the user 
  * Inject the current user 
  * Other models 
  * Code size 
  * Recap 

  1. [ FastAPI  ](../../..)
  2. [ Learn  ](../../../learn/)
  3. [ Tutorial - User Guide  ](../../)
  4. [ Security  ](../)

#  Get Current User  ¶

In the previous chapter the security system (which is based on the dependency
injection system) was giving the _path operation function_ a ` token ` as a `
str ` :

Python 3.9+  Python 3.8+  Python 3.8+ non-Annotated

    
    
    from typing import Annotated
    
    from fastapi import Depends, FastAPI
    from fastapi.security import OAuth2PasswordBearer
    
    app = FastAPI()
    
    oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
    
    
    @app.get("/items/")
    async def read_items(token: Annotated[str, Depends(oauth2_scheme)]):
        return {"token": token}
    
    
    
    from fastapi import Depends, FastAPI
    from fastapi.security import OAuth2PasswordBearer
    from typing_extensions import Annotated
    
    app = FastAPI()
    
    oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
    
    
    @app.get("/items/")
    async def read_items(token: Annotated[str, Depends(oauth2_scheme)]):
        return {"token": token}
    

Tip

Prefer to use the ` Annotated ` version if possible.

    
    
    from fastapi import Depends, FastAPI
    from fastapi.security import OAuth2PasswordBearer
    
    app = FastAPI()
    
    oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
    
    
    @app.get("/items/")
    async def read_items(token: str = Depends(oauth2_scheme)):
        return {"token": token}
    

But that is still not that useful.

Let's make it give us the current user.

##  Create a user model  ¶

First, let's create a Pydantic user model.

The same way we use Pydantic to declare bodies, we can use it anywhere else:

Python 3.10+  Python 3.9+  Python 3.8+  Python 3.10+ non-Annotated  Python
3.8+ non-Annotated

    
    
    from typing import Annotated
    
    from fastapi import Depends, FastAPI
    from fastapi.security import OAuth2PasswordBearer
    from pydantic import BaseModel
    
    app = FastAPI()
    
    oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
    
    
    class User(BaseModel):
        username: str
        email: str | None = None
        full_name: str | None = None
        disabled: bool | None = None
    
    
    def fake_decode_token(token):
        return User(
            username=token + "fakedecoded", email="john@example.com", full_name="John Doe"
        )
    
    
    async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]):
        user = fake_decode_token(token)
        return user
    
    
    @app.get("/users/me")
    async def read_users_me(current_user: Annotated[User, Depends(get_current_user)]):
        return current_user
    
    
    
    from typing import Annotated, Union
    
    from fastapi import Depends, FastAPI
    from fastapi.security import OAuth2PasswordBearer
    from pydantic import BaseModel
    
    app = FastAPI()
    
    oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
    
    
    class User(BaseModel):
        username: str
        email: Union[str, None] = None
        full_name: Union[str, None] = None
        disabled: Union[bool,