Skip to content

Commit ca1cc3c

Browse files
committed
GH-9: Add Auth to on-auth callback
1 parent 6cc5b61 commit ca1cc3c

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

examples/demonstration/main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from database import Base
77
from database import engine
88
from database import get_db
9+
from fastapi_oauth2.middleware import Auth
910
from fastapi_oauth2.middleware import OAuth2Middleware
1011
from fastapi_oauth2.middleware import User
1112
from fastapi_oauth2.router import router as oauth2_router
@@ -17,7 +18,7 @@
1718
router = APIRouter()
1819

1920

20-
async def on_auth(user: User):
21+
async def on_auth(auth: Auth, user: User):
2122
# perform a check for user existence in
2223
# the database and create if not exists
2324
db: Session = next(get_db())

src/fastapi_oauth2/middleware.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class OAuth2Backend(AuthenticationBackend):
124124
def __init__(
125125
self,
126126
config: OAuth2Config,
127-
callback: Callable[[User], Union[Awaitable[None], None]] = None,
127+
callback: Callable[[Auth, User], Union[Awaitable[None], None]] = None,
128128
) -> None:
129129
Auth.set_http(config.allow_http)
130130
Auth.set_secret(config.jwt_secret)
@@ -153,7 +153,7 @@ async def authenticate(self, request: Request) -> Optional[Tuple[Auth, User]]:
153153

154154
# Call the callback function on authentication
155155
if callable(self.callback):
156-
coroutine = self.callback(user)
156+
coroutine = self.callback(auth, user)
157157
if issubclass(type(coroutine), Awaitable):
158158
await coroutine
159159
return auth, user
@@ -168,7 +168,7 @@ def __init__(
168168
self,
169169
app: ASGIApp,
170170
config: Union[OAuth2Config, dict],
171-
callback: Callable[[User], Union[Awaitable[None], None]] = None,
171+
callback: Callable[[Auth, User], Union[Awaitable[None], None]] = None,
172172
**kwargs, # AuthenticationMiddleware kwargs
173173
) -> None:
174174
"""Initiates the middleware with the given configuration.

0 commit comments

Comments
 (0)