Basic authentication with multiple usernames filtering database

I have an SQL database that stores all my data. I have a column there called “client_name” where I have client1, client2, etc.

I have created a basic dash authentication where as soon as my dash application loads, it asks the user for a username and password.

How can I make it work that if the user inputs “client1” as username, the app will automatically filter my SQL database to only read rows that belong to client1 and thus all visuals will display client1’s data?

Sample code:

# User dictionary
USERNAME_PASSWORD_PAIRS = {
     'client1': 'client1'
    , 'client2': 'client2'
}

# Basic authentication
auth = dash_auth.BasicAuth(
    app,
    USERNAME_PASSWORD_PAIRS
)


# Connect and read data from SQL server
odbc_params = f'DRIVER={driver};SERVER=tcp:{server};PORT=1433;DATABASE={database};UID={username};PWD={password}'
connection_string = f'mssql+pyodbc:///?odbc_connect={odbc_params}'
engine = create_engine(connection_string)
query = "SELECT * FROM [dbo].[test]"  # database with all client data
df = pd.read_sql(query, engine)
engine.dispose()

So I want the “df” to read the filtered dataframe based on wether “client1” or “client2” was used to log in.

Thanks for the help

Hello @GabrielBKaram,

I dont know if there is a way to readily get the user or whoever logs in via Basic Auth.

However, via flask_login you can access the user info using current_user.

If you are tied to Basic Auth, then I am not sure there is a fix.

Hi @jinnyzor ,

Thanks for the reply. Is there some documentation you can share with me for flask_login?

Thanks

Sure thing, please check this thread:

There is some import info about how you would go about locking down the application to keep it from responding to bad POST requests in the later replies.

hi @GabrielBKaram ,

FYI