build(auth.py): send redirect url via magic link

This commit is contained in:
Krrish Dholakia 2023-12-23 17:35:19 +05:30
parent 1bfab87323
commit 189fcc0934
3 changed files with 18 additions and 8 deletions

View file

@ -1,11 +1,13 @@
""" """
Admin sets proxy url + allowed email subdomain Admin sets proxy url + allowed email subdomain
""" """
from dotenv import load_dotenv
load_dotenv()
import streamlit as st import streamlit as st
import base64 import base64, os
# Replace your_base_url with the actual URL where the proxy auth app is hosted # Replace your_base_url with the actual URL where the proxy auth app is hosted
your_base_url = 'http://localhost:8501' # Example base URL your_base_url = os.getenv("BASE_URL") # Example base URL
# Function to encode the configuration # Function to encode the configuration
def encode_config(proxy_url, allowed_email_subdomain): def encode_config(proxy_url, allowed_email_subdomain):

View file

@ -1,8 +1,10 @@
""" """
Routes between admin, auth, keys pages Routes between admin, auth, keys pages
""" """
from dotenv import load_dotenv
load_dotenv()
import streamlit as st import streamlit as st
import base64, binascii import base64, binascii, os
from admin import admin_page from admin import admin_page
from auth import auth_page from auth import auth_page
from urllib.parse import urlparse, parse_qs from urllib.parse import urlparse, parse_qs
@ -44,7 +46,7 @@ if page_param:
try: try:
# Try to decode the page_param from base64 # Try to decode the page_param from base64
if is_base64(page_param): if is_base64(page_param):
auth_page() auth_page(redirect_url=f"{os.getenv('BASE_URL')}/{page_param}")
else: else:
st.error("Unknown page") st.error("Unknown page")
except Exception as e: except Exception as e:

View file

@ -5,6 +5,8 @@ Uses supabase passwordless auth: https://supabase.com/docs/reference/python/auth
Remember to set your redirect url to 8501 (streamlit default). Remember to set your redirect url to 8501 (streamlit default).
""" """
import logging
logging.basicConfig(level=logging.DEBUG)
import streamlit as st import streamlit as st
from dotenv import load_dotenv from dotenv import load_dotenv
load_dotenv() load_dotenv()
@ -17,14 +19,18 @@ key: str = os.environ.get("SUPABASE_KEY")
supabase: Client = create_client(url, key) supabase: Client = create_client(url, key)
def sign_in_with_otp(email: str): def sign_in_with_otp(email: str, redirect_url: str):
data = supabase.auth.sign_in_with_otp({"email": email}) data = supabase.auth.sign_in_with_otp({"email": email,
"options": {
"email_redirect_to": redirect_url
}})
print(f"data: {data}")
# Redirect to Supabase UI with the return data # Redirect to Supabase UI with the return data
st.write(f"Please check your email for a login link!") st.write(f"Please check your email for a login link!")
# Create the Streamlit app # Create the Streamlit app
def auth_page(): def auth_page(redirect_url: str):
st.title("User Authentication") st.title("User Authentication")
# User email input # User email input
@ -32,4 +38,4 @@ def auth_page():
# Sign in button # Sign in button
if st.button("Sign In"): if st.button("Sign In"):
sign_in_with_otp(email) sign_in_with_otp(email, redirect_url=redirect_url)