Documentation for TokenAuth and implementing endpoints for login, register, reset #795
Replies: 11 comments 8 replies
-
The docs aren't great for TokenAuth, but this is the endpoint you need for logging in and providing a token: https://github.com/piccolo-orm/piccolo_api/blob/master/piccolo_api/token_auth/endpoints.py You'll have to mount that endpoint to a public route in FastAPI. To create the tables, add |
Beta Was this translation helpful? Give feedback.
-
Would you happen to have some example code for how to do add that ? Will it be mounted in app.py? It would be good if you had some sample code for login and register Thanks again @dantownsend 🙇♂️ |
Beta Was this translation helpful? Give feedback.
-
Thanks. Let me try that. Also, if I have a route like
How can I make that protected by requiring a token like access token? Will the access token be returned in the /login call? Is there a decorator way to protect the |
Beta Was this translation helpful? Give feedback.
-
Was trying to implement the TokenAuth using the steps you mentioned above This is what I have in app.py so far
This is based on the Session Auth code you provided and on https://github.com/piccolo-orm/piccolo_api/blob/master/piccolo_api/token_auth/endpoints.py However, it seems, that the token_auth/endpoints middleware does not have token_login and token_logout methods Any ideas on what is wrong with the above code? |
Beta Was this translation helpful? Give feedback.
-
Updated the code as below -
However, I'm still getting the error -
There doesn't seem to be a login function in Regarding |
Beta Was this translation helpful? Give feedback.
-
I apologize for intruding into this discussion, but I will try to help. @arunabhdas I think you are mixing from piccolo_api.token_auth.endpoints import TokenAuthLoginEndpoint
from piccolo_api.token_auth.middleware import (
TokenAuthBackend,
PiccoloTokenAuthProvider,
)
from piccolo_api.token_auth.tables import TokenAuth
public_app = FastAPI(
routes=[
Mount(
"/admin/",
create_admin(
tables=[Manager, TokenAuth]
),
),
Route("/login/", TokenAuthLoginEndpoint),
],
)
private_app = FastAPI()
protected_app = AuthenticationMiddleware(
private_app,
backend=TokenAuthBackend(PiccoloTokenAuthProvider()),
)
FastAPIWrapper(
"/projects/",
fastapi_app=private_app,
piccolo_crud=PiccoloCRUD(Manager, read_only=False),
fastapi_kwargs=FastAPIKwargs(
all_routes={"tags": ["Project"]},
),
)
public_app.mount("/private", protected_app) After that, you have to create a token for the user in the
and result is
Hope that helps. @dantownsend We should change docs for app = AuthenticationMiddleware(
my_asgi_app,
backend=TokenAuthBackend(PiccoloTokenAuthProvider()),
) because without |
Beta Was this translation helpful? Give feedback.
-
Thanks @sinisaos for your help on this thread. So I did what you suggested and modified app.py as below
Modified main.py to run public_app as below
Now when I run using However, when I try to add a token for user And the error in the logs is
So I'm wondering, do I have to create a relation between BaseUser and TokenAuth and do a migration? Not sure what I'm doing wrong. I checked the migrations also
Updated source with the above changes here Your help and inputs are greatly appreciated @dantownsend and @sinisaos 🙏 |
Beta Was this translation helpful? Give feedback.
-
Just a quick update on this thread and thanks to @sinisaos and @dantownsend for your help and patience. It seems I had to do the additional step after declaring the private and protected routes -
After doing the above step, things work correctly as @sinisaos described !! However, it seems my API docs endpoint at /docs has gone away - How can I bring back the FastAPI doc at Have a few more questions on this same thread -
Thanks again for all the support 🙏 |
Beta Was this translation helpful? Give feedback.
-
I was using Postman to test those endpoints and pass the Bearer token. Was still hoping for more detailed docs for JWT auth if you have time since my use-case is to have refresh and access tokens for the mobile user (that can be made to expire after logout / timeout) |
Beta Was this translation helpful? Give feedback.
-
Thanks @sinisaos 🙏 Appreciate all the help 💯 |
Beta Was this translation helpful? Give feedback.
-
The docs for JWT and Token Auth have now been updated. |
Beta Was this translation helpful? Give feedback.
-
Hi @dantownsend
Have managed to get the admin section working for my app here
https://github.com/arunabhdas/unicorn-fullstack/blob/main/unicorn-fullstack-fastapi-piccolo/piccolo/README.md
Am able to get the admin up and running with your help
Since I need to use this piccolo API as a backend for my mobile app, I need to implement the /login and /register endpoints in a way that supports token auth.
I tried to look at the docs here
However, I am not able to figure out what my /login and /register endpoint implementation should look like if I would like to authenticate using the BaseUser (PiccoloUser) model class.
Could you help @dantownsend ?
Please note that my app.py currently looks as below
And I would like to add TokenAuth to support my mobile app login and registration.
Thanks in advance for your help 🙇
Beta Was this translation helpful? Give feedback.
All reactions