Add authentication and user management to your dash app.
This adds a user database and login page on front of your Dash app.
You can import users from a csv file, or create them in the app itself by signing in as an admin user and going to the /create
page.
The log in page:
There is a provision made for 'roles', currently there is just 'admin', but using the same routing tree (the if/elif/else found in main.py) you could make as many roles as needed for restricting access to any of the directories.
Here we limit access to the /create
path to just admins, but the same logic applies to any number of roles and pages.
if pathname == f'{url_prefix[:-1]}/create':
if current_user.is_authenticated:
user = Users.query.filter_by(id=current_user.id).first()
if user.role == "admin":
return create
else:
return 'User does not have admin permissions'
else:
return login
This page is where you create users: /create
Roles is where you can specify the access level of the user.
You can add as many access levels as you like, but the built in one is 'admin' or 'default'.
After logging in user will see this page:
After selecting a dash that is only available to a logged in user, they will see this page:
Must be run in Linux terminal, requires make
The makefile contains all the commands needed to set up and run the example dash app.
First we need to set the admin password in the makefile
This password will be used ad the first user login for the system, from which other users are created.
Create a database for users:
make create-database
Run the dash:
make run
Now you can open the dash to see the login page.
You can create users here: /create
Load a csv of users into the database.
The filename of the csv this loads can be edited in the makefile.
make load-users-csv
Check the contents of the user database:
make display-database
It will print a list like this: