MushroomMaula / fastapi_login

FastAPI-Login tries to provide similar functionality as Flask-Login does.
https://pypi.org/project/fastapi-login
MIT License
639 stars 58 forks source link

Get current user without throwing an error #99

Closed mtkgeek closed 1 year ago

mtkgeek commented 1 year ago

Hi, great package. Is there a way to get the current user without throwing the NotAuthenticatedException. Just get the user or None in my case

Writing a custom auth_exception_handler doesn't cut it for me because it expects a callable to be returned so I can't return None. This might look trivial but in the case where I'm building a web app and my responses class are templates not jsonresponses so returning a jsonresponse also doesn't help because internally fastapi_login just returns that to the browser instead of passing that as a return value from calling get_current_user. What I'm saying in essence is when I call

   ` user = await manager.get_current_user(token=request.cookies.get('token'))`

I should get a value for user from the above, whether that be a user object, None, Jsonresponse. Not currently where I only get a user or nothing else(an exception that I can't control or customize)

MushroomMaula commented 1 year ago

I'm afraid thats not possible out of the box with the dependency however you could in theory use a middleware. If that doesn't work have a look at #91 for some suggestions on how to implement this behaviour.

MushroomMaula commented 1 year ago

Now available in the latest release. See here for an example.