Is it bad idea to have something like this done for default admin user? https://github.com/Nipa-Dev/lemon-API/blob/main/postgres/init.sql#L27
Obviously not really secure in any way, but is there a way to create this admin user based on ENV variables? And how would I do that as password hash is required, which is done in here: https://github.com/Nipa-Dev/lemon-API/blob/main/lemonapi/utils/auth.py#L88-L102
or would it make more sense to try something else, maybe trying to initialize the account in app startup event?
#๐ FastAPI default admin account creation for PostgreSQL database
33 messages ยท Page 1 of 1 (latest)
@full marten
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
also, what about the SQL queries in general, should something be changed/done differently?
You could mimic how Django does it. After deploy, manually create an admin account with py manage.py createsuperuser and then create the admin account
Or use Django instead of re-implementing it in FastAPI?
I don't plan on changing the framework at this point. Would the manage.py require user input?
if so, as this is deployed and developed to be ran in docker, it could prove problematic
hmmm... There could be a script that connects to the database and executes user creation via ENV variables maybe? As user interactions would be a bit too hard to implement
setup hook?
Discord bot has this thing called a setup hook
As soon as it boots up, this is the first thing it does before it continuously runs its main function
the setup hook is only called once
and only on start up
hmmm, I kind of need the app to be running first as the password creation process is required and that needs the python functions in the API
can you share how the password creation process works?
https://github.com/Nipa-Dev/lemon-API/blob/main/lemonapi/utils/auth.py#L88-L102 this is ran to generate the hash for password
that can happen afterwards but when the app is booted up, you should be able to call this function to create the password
the startup event could be a place for it
class Bla:
def setup_hook():
# Create table
# Create admin and password from ENV
# Call password function hashing thingy
The password function can be a static method
So its not tied down to creating/init a class object that holds it
i.e. if you're using classes
I guess something like this could work here after the pool is initialized,https://github.com/Nipa-Dev/lemon-API/blob/main/lemonapi/main.py#L48
# get env variables somehow
password = env_password
username = env_username
# get required values
pw_hash = get_password_hash(password)
# insert values to table
...
lemonapi/main.py line 48
await Connection.DB_POOL```
or I might be able to call this directly in startup as well: https://github.com/Nipa-Dev/lemon-API/blob/main/lemonapi/endpoints/security.py#L125
I think I know what to do now! this is what I need to call to make sure it's going to work well (or I just create shortened mechanism of this to the startup) https://github.com/Nipa-Dev/lemon-API/blob/e786f2c41d8e21d11d0c572687883368d89511c9/lemonapi/utils/crud.py#L96
lemonapi/utils/crud.py line 96
async def add_user(self, user: auth.NewUser):```
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.