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

Don't recreate the credentials dictionary #238

Open
traverseda opened this issue Oct 28, 2024 · 2 comments
Open

Don't recreate the credentials dictionary #238

traverseda opened this issue Oct 28, 2024 · 2 comments
Labels
enhancement New feature or request

Comments

@traverseda
Copy link

You can get steamlit-authenticator to automatically save user accounts using a construct like this:

credential_store = {'usernames':shelve.open("./user_accounts.shelve")}

authenticator = stauth.Authenticate(
        credentials=credential_store,
)

This pairs even better with tools like zict

storage = zict.File(directory="./accounts")
utf8_to_bytes = zict.Func(lambda x: x.encode('utf8'), lambda x: x.decode('utf8'), storage)
encoder = zict.Func(yaml.safe_dump, yaml.safe_load, utf8_to_bytes)
credential_store = {'usernames':encoder}

Which will create a new yaml file for each user. You can also use sqlite, or lmdb, or even postgres transports.

But none of that works because instead of taking the actual credentials object at face value you recreate it using a standard python dict.

@traverseda traverseda changed the title Don't recreate the dictionary Don't recreate the credentials dictionary Oct 28, 2024
@mkhorasani mkhorasani added the enhancement New feature or request label Oct 28, 2024
@mkhorasani
Copy link
Owner

Thank you @traverseda I will take this into consideration.

@traverseda
Copy link
Author

traverseda commented Oct 28, 2024

This is it for my lunch break, but here's how you can do this regardless with zict, monkey patch style.

storage = zict.File(directory="./accounts")
utf8_to_bytes = zict.Func(lambda x: x.encode('utf8'), lambda x: x.decode('utf8'), storage)
encoder = zict.Func(yaml.safe_dump, yaml.safe_load, utf8_to_bytes)
credential_store = {'usernames':encoder}

authenticator = stauth.Authenticate(
        credentials={'usernames':[]},
)
authenticator.authentication_controller.authentication_model.credentials=credential_store

This will automatically store account data when it's updated, it's relatively efficient and should scale fine to real databases or other key-value stores.

Recommend against using Shelve in production

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

No branches or pull requests

2 participants