Skip to content

Commit 966ff91

Browse files
committed
add backend Authenticator FastAPIUsers readme
1 parent 4fcf078 commit 966ff91

1 file changed

Lines changed: 72 additions & 0 deletions

File tree

README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,78 @@ def get_jwt_strategy() -> JWTStrategy:
186186
)
187187
```
188188

189+
### Backend
190+
191+
auth_backend собирает `Transport` и `Strategy` воединно в `AuthenticationBackend`
192+
Этот класс необхим будет для:
193+
194+
- `Authenticator`
195+
- `FastAPIUsers`
196+
197+
```python
198+
from fastapi_users.authentication import AuthenticationBackend
199+
200+
from config import settings
201+
202+
203+
auth_backend = AuthenticationBackend(
204+
name=settings.JWT.NAME,
205+
transport=bearer_transport,
206+
get_strategy=get_jwt_strategy,
207+
)
208+
```
209+
210+
### Authenticator
211+
212+
Authenticator необходим для `Callback[Depenpency]` пользователей,
213+
применяется для вытаскивания текущего пользователя.
214+
215+
```python
216+
from fastapi import Depends
217+
from fastapi_users.authentication import Authenticator
218+
219+
from config.models import User
220+
221+
222+
authenticator = Authenticator(
223+
(auth_backend,),
224+
get_user_manager,
225+
)
226+
227+
active_user = authenticator.current_user(
228+
active=True,
229+
verified=True,
230+
)
231+
232+
async def get_user(user: User = Depends(active_user)) -> User:
233+
return user
234+
```
235+
236+
### FastAPIUsers
237+
238+
FastAPIUsers применяется в итоговом выводе End-points в API
239+
240+
```python
241+
from fastapi_users import FastAPIUsers
242+
243+
from api_v1.auth.schemas import UserRead, UserUpdate
244+
245+
246+
fastapi_users = FastAPIUsers[User, int](
247+
get_user_manager,
248+
(auth_backend,)
249+
)
250+
251+
router = APIRouter()
252+
253+
router.include_router(fastapi_users.get_users_router(UserRead, UserUpdate),
254+
tags=['Users'],
255+
prefix='/users',
256+
)
257+
```
258+
259+
По итогу будет добавлены новые End-points в API
260+
189261
## Найболее используемые
190262

191263
Найболее используемые конструкции с которыми приходится часто взаимодействовать.

0 commit comments

Comments
 (0)