fastapi_hf / get_token.py
looh2's picture
feat: Add script for user authentication and token retrieval from Supabase
3241cab
Raw
History Blame Contribute Delete
1.55 kB
"""
Usage:
python get_token.py
python get_token.py your@email.com yourpassword
"""
import sys
import os
from dotenv import load_dotenv
load_dotenv()
SUPABASE_URL = os.getenv("SUPABASE_URL", "")
SUPABASE_ANON_KEY = os.getenv("SUPABASE_ANON_KEY", "")
if not SUPABASE_URL or not SUPABASE_ANON_KEY:
print("ERROR: SUPABASE_URL and SUPABASE_ANON_KEY must be set in .env")
sys.exit(1)
if len(sys.argv) == 3:
email, password = sys.argv[1], sys.argv[2]
else:
email = input("Email: ").strip()
password = input("Password: ").strip()
from supabase import create_client
from supabase_auth.errors import AuthApiError
sb = create_client(SUPABASE_URL, SUPABASE_ANON_KEY)
try:
res = sb.auth.sign_in_with_password({"email": email, "password": password})
except AuthApiError as e:
print(f"\nAuth failed: {e.message}")
print("Check your email/password, or confirm the account in the Supabase dashboard.")
sys.exit(1)
if not res.session:
print("\nSign-in returned no session. The account may not be confirmed yet.")
print("Go to Supabase Dashboard β†’ Authentication β†’ Users and confirm the user.")
sys.exit(1)
if not res.user:
print("\nSign-in returned no user. Check your email/password or account confirmation status.")
sys.exit(1)
token = res.session.access_token
user_id = res.user.id
print(f"\nUser ID : {user_id}")
print(f"\nBearer token:\n{token}\n")
print("Use in Swagger: click Authorize β†’ paste the token above.")
print("Use in curl: -H \"Authorization: Bearer <token>\"")