Spaces:
Sleeping
Sleeping
File size: 5,219 Bytes
daa81fb 486c2a9 daa81fb 486c2a9 daa81fb 486c2a9 daa81fb 486c2a9 daa81fb 486c2a9 daa81fb 486c2a9 daa81fb 8f0f2c2 daa81fb 950f1d6 daa81fb 950f1d6 59b48be daa81fb 950f1d6 daa81fb 950f1d6 59b48be daa81fb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
import os
import base64
import streamlit as st
from app.db import supabase
def auth_view():
"""Render Supabase authentication with Login, Register, and Forgot Password tabs."""
# Wrapper (centered) for all auth content
left, center, right = st.columns([1, 2, 1])
with center:
# Header: smaller PNP logo and centered title
# Path logo
logo_path = os.path.join("assets", "pnp-logo.png")
# Convert ke base64 biar bisa di-embed langsung
def get_base64_image(path):
with open(path, "rb") as f:
return base64.b64encode(f.read()).decode()
encoded_logo = get_base64_image(logo_path)
# Render dalam satu div center
st.markdown(
f"""
<div style="text-align:center;">
<img src="data:image/png;base64,{encoded_logo}" width="100">
<h1 style="margin-top:0.25rem;">PNP bot</h1>
</div>
""",
unsafe_allow_html=True
)
# Auth tabs inside wrapper
tab_login, tab_register, tab_forgot = st.tabs(["Login", "Register", "Forgot Password"])
with tab_login:
with st.form("login_form"):
email = st.text_input("Email")
password = st.text_input("Password", type="password")
submitted = st.form_submit_button("Login")
if submitted:
# Demo password fallback
shared_pw = os.getenv("APP_DEMO_PASSWORD")
if shared_pw and password == shared_pw:
st.session_state["user"] = {"id": "demo-user", "email": email or "demo@example.com"}
st.success("Login demo berhasil")
st.rerun()
try:
auth_res = supabase.auth.sign_in_with_password({
"email": email,
"password": password,
})
user = getattr(auth_res, "user", None)
if user:
st.session_state["user"] = {"id": user.id, "email": getattr(user, "email", email)}
session_obj = getattr(auth_res, "session", None)
if session_obj:
st.session_state["sb_session"] = {
"access_token": getattr(session_obj, "access_token", None),
"refresh_token": getattr(session_obj, "refresh_token", None),
}
st.success("Login berhasil")
st.rerun()
else:
st.error("Email atau password salah.")
except Exception as e:
st.error(f"Gagal login: {e}")
with tab_register:
st.caption("Buat akun baru. Anda akan menerima email konfirmasi.")
with st.form("register_form"):
r_email = st.text_input("Email", key="reg_email")
r_password = st.text_input("Password", type="password", key="reg_password")
r_password2 = st.text_input("Konfirmasi Password", type="password", key="reg_password2")
submitted_r = st.form_submit_button("Register")
if submitted_r:
if r_password != r_password2:
st.error("Password tidak sama.")
else:
try:
# Prefer explicit env, then generic site URL, then production URL
redirect_url = os.getenv(
"SUPABASE_EMAIL_REDIRECT",
os.getenv("NEXT_PUBLIC_SITE_URL", "https://yozora721-pnp-chatbot-v1.hf.space"),
) + "/reset_password"
supabase.auth.sign_up({
"email": r_email,
"password": r_password,
"options": {"email_redirect_to": redirect_url}
})
st.success("Registrasi berhasil. Silakan cek email untuk konfirmasi.")
except Exception as e:
st.error(f"Gagal registrasi: {e}")
with tab_forgot:
st.caption("Kirim tautan reset password ke email Anda.")
with st.form("forgot_form"):
f_email = st.text_input("Email", key="forgot_email")
submitted_f = st.form_submit_button("Kirim Link Reset")
if submitted_f:
try:
# Prefer explicit env, then generic site URL, then production URL
redirect_url = os.getenv(
"SUPABASE_EMAIL_REDIRECT",
os.getenv("NEXT_PUBLIC_SITE_URL", "https://yozora721-pnp-chatbot-v1.hf.space"),
) + "/reset_password"
supabase.auth.reset_password_for_email(f_email, {"redirect_to": redirect_url})
st.success("Email reset password telah dikirim. Periksa kotak masuk Anda.")
except Exception as e:
st.error(f"Gagal mengirim email reset password: {e}")
|