-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdependencies.py
More file actions
32 lines (23 loc) · 826 Bytes
/
dependencies.py
File metadata and controls
32 lines (23 loc) · 826 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from typing import Annotated
from fastapi import Depends
from fastapi.security import OAuth2PasswordBearer
from auth.jwt import decode_access_token, credentials_exception
from db.crud.user import get_user_by_id
from db.db import SessionLocal
from db.models.user import SqlUser
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="auth/token")
# Dependency
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)], db: SessionLocal = Depends(get_db)):
token_data = decode_access_token(token)
user = get_user_by_id(db, token_data.id)
if user is None:
raise credentials_exception
return user
async def is_admin(user: Annotated[SqlUser, Depends(get_current_user)]):
return user.is_admin