Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
pessimo opened this issue Dec 30, 2024 · 1 comment

Comments

@pessimo
Copy link

pessimo commented Dec 30, 2024

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?

@Shivakishore14
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants
@pessimo @Shivakishore14 and others