#๐Ÿ”’ FastAPI default admin account creation for PostgreSQL database

33 messages ยท Page 1 of 1 (latest)

full marten
#

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?

tacit violetBOT
#

@full marten

Python help channel opened

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.

full marten
#

also, what about the SQL queries in general, should something be changed/done differently?

lament lichen
#

Or use Django instead of re-implementing it in FastAPI?

full marten
#

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

full marten
lament lichen
#

sure if you want

#

something like a setup hook

full marten
#

setup hook?

lament lichen
#

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

full marten
#

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

lament lichen
#

can you share how the password creation process works?

full marten
lament lichen
full marten
#

the startup event could be a place for it

lament lichen
#
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

full marten
tacit violetBOT
#

lemonapi/main.py line 48

await Connection.DB_POOL```
full marten
tacit violetBOT
#

lemonapi/utils/crud.py line 96

async def add_user(self, user: auth.NewUser):```
full marten
#

anyway! Thanks for ideas ๐Ÿ˜„

#

!close thank you!

tacit violetBOT
#
Python help channel closed

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.