Kinda decided against using the default auth system of Plotly and built my own, to be fair I never really gave plotly auth a try lol I do use multi pages and have achieved private authentication from a Django backend. The way I was able to do this was to create a Django project and setup an API (using ninja), DRF or Graphql or any other API provider should work but the code going to look a little different.
Then I created a class and a route to the URL via Django and the views.py like so:
from ninja import NinjaAPI
from ninja.security import HttpBearer
from ninja.security import HttpBasicAuth
from django.contrib.auth.models import User
from django.contrib.auth import authenticate as auth_authenticate
api = NinjaAPI()
class BasicAuth(HttpBasicAuth):
def authenticate(self, request, username, password):
# Request all Users on Application
users = User.objects.all()
# check username vs user in database
user_in_users = users.values_list('username', flat=True)
# print('user_in_users', list(user_in_users))
emails = users.values_list('email', flat=True)
# print('emails', list(emails))
if username in list(user_in_users):
user = auth_authenticate(username=username, password=password)
if user is not None:
# the credentials are valid
return username
else:
# the credentials are invalid
return None
elif username in list(emails):
print('Wasn\'t able to find Username, trying email')
if username in emails:
print('Email in database')
u = User.objects.get(email=username)
user = auth_authenticate(username=u, password=password)
if user is not None:
# the credentials are valid
return username
else:
print('Username / Email not in database')
# the credentials are invalid
return None
@api.get("/login", auth=BasicAuth())
def basic(request):
return {"httpuser": request.auth}
Labeled my Login in the Django urls.py:
from django.urls import path, include
from account import views
urlpatterns = [
path('login', views.login, name='login_page'),
path('profile', views.profile, name='profile_page'),
path('register', views.register, name='register_page'),
]
Then on my dash side of things I setup a method to send a request to the API:
import requests
import colorama
def login(username, password):
url_login = 'http://127.0.0.1:8000/api/login'
response = requests.get(url_login, auth=(username, password))
print(colorama.Fore.YELLOW + f"{response.status_code}")
print(colorama.Fore.WHITE + f"{response.headers}")
print(colorama.Fore.GREEN + f"{response.content}")
print(colorama.Fore.RESET + response.text)
if response.status_code == 200:
print(colorama.Fore.GREEN + f"Login Successful")
print(colorama.Fore.RESET)
return True
else:
print(colorama.Fore.RED + f"Login Failed")
print(colorama.Fore.RESET)
return False
if __name__ == '__main__':
login('test@gmail.com', 'NeverTellMeTheOdds')
Lastly on my app.py I created a callback event that calls the function login and based on the response changes the application to either login user or inform them they don’t have an account with us. Might be a little more complex than the default authentication offered with plotly but I’m after more features and figured I’d share this as you or someone in your same situation might find it useful.