Add get_optional_* dependency callables

This commit is contained in:
François Voron
2020-05-25 08:13:42 +02:00
parent 71ca7d85ed
commit 9508768502
4 changed files with 196 additions and 15 deletions

View File

@ -10,9 +10,9 @@
Get the current user (**active or not**). Will throw a `401 Unauthorized` if missing or wrong credentials.
```py
@app.get('/protected-route')
@app.get("/protected-route")
def protected_route(user: User = Depends(fastapi_users.get_current_user)):
return f'Hello, {user.email}'
return f"Hello, {user.email}"
```
## `get_current_active_user`
@ -20,9 +20,9 @@ def protected_route(user: User = Depends(fastapi_users.get_current_user)):
Get the current active user. Will throw a `401 Unauthorized` if missing or wrong credentials or if the user is not active.
```py
@app.get('/protected-route')
@app.get("/protected-route")
def protected_route(user: User = Depends(fastapi_users.get_current_active_user)):
return f'Hello, {user.email}'
return f"Hello, {user.email}"
```
## `get_current_superuser`
@ -30,9 +30,48 @@ def protected_route(user: User = Depends(fastapi_users.get_current_active_user))
Get the current superuser. Will throw a `401 Unauthorized` if missing or wrong credentials or if the user is not active. Will throw a `403 Forbidden` if the user is not a superuser.
```py
@app.get('/protected-route')
@app.get("/protected-route")
def protected_route(user: User = Depends(fastapi_users.get_current_superuser)):
return f'Hello, {user.email}'
return f"Hello, {user.email}"
```
## `get_optional_current_user`
Get the current user (**active or not**). Will return `None` if missing or wrong credentials. It can be useful if you wish to change the behaviour of your endpoint if a user is logged in or not.
```py
@app.get("/optional-user-route")
def optional_user_route(user: Optional[User] = Depends(fastapi_users.get_optional_current_user)):
if user:
return f"Hello, {user.email}"
else:
return "Hello, anonymous"
```
## `get_optional_current_active_user`
Get the current active user. Will return `None` if missing or wrong credentials. It can be useful if you wish to change the behaviour of your endpoint if a user is logged in or not.
```py
@app.get("/optional-user-route")
def optional_user_route(user: User = Depends(fastapi_users.get_optional_current_active_user)):
if user:
return f"Hello, {user.email}"
else:
return "Hello, anonymous"
```
## `get_optional_current_superuser`
Get the current superuser. Will return `None` if missing or wrong credentials. It can be useful if you wish to change the behaviour of your endpoint if a user is logged in or not.
```py
@app.get("/optional-user-route")
def optional_user_route(user: User = Depends(fastapi_users.get_optional_current_superuser)):
if user:
return f"Hello, {user.email}"
else:
return "Hello, anonymous"
```
## In path operation
@ -40,9 +79,9 @@ def protected_route(user: User = Depends(fastapi_users.get_current_superuser)):
If you don't need a user, you can use more clear way:
```py
@app.get('/protected-route', dependencies=[Depends(fastapi_users.get_current_superuser)])
@app.get("/protected-route", dependencies=[Depends(fastapi_users.get_current_superuser)])
def protected_route():
return 'Hello, some user.'
return "Hello, some user."
```
You can read more about this [in FastAPI docs](https://fastapi.tiangolo.com/tutorial/dependencies/dependencies-in-path-operation-decorators/).