mirror of
https://github.com/fastapi-users/fastapi-users.git
synced 2026-03-13 07:49:55 +08:00
Improve typing and make User pydantic models dynamic
This commit is contained in:
@@ -3,7 +3,7 @@ from typing import Callable
|
||||
from starlette.responses import Response
|
||||
|
||||
from fastapi_users.db import BaseUserDatabase
|
||||
from fastapi_users.models import UserDB
|
||||
from fastapi_users.models import BaseUserDB
|
||||
|
||||
|
||||
class BaseAuthentication:
|
||||
@@ -13,8 +13,8 @@ class BaseAuthentication:
|
||||
def __init__(self, user_db: BaseUserDatabase):
|
||||
self.user_db = user_db
|
||||
|
||||
async def get_login_response(self, user: UserDB, response: Response):
|
||||
async def get_login_response(self, user: BaseUserDB, response: Response):
|
||||
raise NotImplementedError()
|
||||
|
||||
def get_authentication_method(self) -> Callable[..., UserDB]:
|
||||
def get_authentication_method(self) -> Callable[..., BaseUserDB]:
|
||||
raise NotImplementedError()
|
||||
|
||||
@@ -7,7 +7,7 @@ from starlette import status
|
||||
from starlette.responses import Response
|
||||
|
||||
from fastapi_users.authentication import BaseAuthentication
|
||||
from fastapi_users.models import UserDB
|
||||
from fastapi_users.models import BaseUserDB
|
||||
|
||||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/login")
|
||||
|
||||
@@ -30,7 +30,7 @@ class JWTAuthentication(BaseAuthentication):
|
||||
self.secret = secret
|
||||
self.lifetime_seconds = lifetime_seconds
|
||||
|
||||
async def get_login_response(self, user: UserDB, response: Response):
|
||||
async def get_login_response(self, user: BaseUserDB, response: Response):
|
||||
data = {"user_id": user.id}
|
||||
token = generate_jwt(data, self.lifetime_seconds, self.secret, self.algorithm)
|
||||
|
||||
@@ -44,7 +44,7 @@ class JWTAuthentication(BaseAuthentication):
|
||||
|
||||
try:
|
||||
data = jwt.decode(token, self.secret, algorithms=[self.algorithm])
|
||||
user_id: str = data.get("user_id")
|
||||
user_id = data.get("user_id")
|
||||
if user_id is None:
|
||||
raise credentials_exception
|
||||
except jwt.PyJWTError:
|
||||
|
||||
Reference in New Issue
Block a user