Commit existing codebase
This commit is contained in:
commit
49bc902bb9
24 changed files with 1208 additions and 0 deletions
17
app/auth/middleware.py
Normal file
17
app/auth/middleware.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
from fastapi import Depends, HTTPException
|
||||
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
||||
from jose import JWTError
|
||||
|
||||
from app.auth.service import verify_token
|
||||
from app.user.model import User
|
||||
from app.user.service import get_user_by_id
|
||||
from app.db import get_db
|
||||
|
||||
|
||||
def get_auth_user(auth: HTTPAuthorizationCredentials = Depends(HTTPBearer()), db=Depends(get_db)) -> User:
|
||||
try:
|
||||
decoded_token = verify_token(auth.credentials)
|
||||
auth_user = get_user_by_id(db, int(decoded_token["sub"]))
|
||||
return auth_user
|
||||
except JWTError:
|
||||
raise HTTPException(status_code=403, detail="Invalid token")
|
||||
Loading…
Add table
Add a link
Reference in a new issue