pocketbase icon indicating copy to clipboard operation
pocketbase copied to clipboard

possibly auth token expire? all actions would fail after server up for some time

Open pessimo opened this issue 1 year ago • 1 comments

I got a server with fastapi and start it in docker. In my project, I would init a client and do an admin auth when server is up

# in common.py file
pb = PB('https://xxxxxxxxxxxxx')
user_data = pb.admins.auth_with_password('xxxxxxxx', 'xxxxxxxx')

and everytime i need to use pb client, I would import user_data from common.py.

but after a specific amount of time(currently i have no idea how long it would be), all the pocketbase actions (like auth、query、update) would fail. And if I restart the server container, everything will be fine.

could anyone provide some idea?

pessimo avatar Dec 30 '24 01:12 pessimo

This is expected, the auth tokens expire after a set amount of time.

We have to handle reauthentication at our side.

As a best practice with fast API instead of putting it in commons use dependencies https://fastapi.tiangolo.com/tutorial/dependencies/

Here is a sample from my code base

# in deps.py

@cached(cache=TTLCache(maxsize=1024, ttl=300))
def __get_pb_client():
    print("getting pb client")
    client = PocketBase(settings.PB_URL)
    admin_data = client.admins.auth_with_password(settings.PB_USERNAME, settings.PB_PASSWORD)
    if not admin_data.is_valid:
        raise Exception("Invalid PB credentials")
    return client


def get_pb_client() -> PocketBase:
    return __get_pb_client()


PBClientDep = Annotated[PocketBase, Depends(get_pb_client)]

# in api file

@router.get("/")
async def get_video(
    pb_client: PBClientDep
):
    video_response = pb_client.collection("video").get_list()
    return video_response

Shivakishore14 avatar Jan 12 '25 21:01 Shivakishore14