Spaces:
Sleeping
Sleeping
| import os | |
| import json | |
| import gradio as gr | |
| from datetime import datetime | |
| import firebase_admin | |
| from firebase_admin import credentials, auth | |
| import secrets | |
| # Firebase configuration | |
| FIREBASE_CONFIG = { | |
| "apiKey": "AIzaSyA-HtFUWNpU6jEe4Cg6LQQSwartBtiqf1o", | |
| "authDomain": "legalease-7cb84.firebaseapp.com", | |
| "projectId": "legalease-7cb84", | |
| "storageBucket": "legalease-7cb84.appspot.com", | |
| "messagingSenderId": "734958314454", | |
| "appId": "1:734958314454:web:43175e12ddcda05fc2fa77" | |
| } | |
| # Current user and time settings | |
| CURRENT_USER = "AkarshanGupta" | |
| CURRENT_TIME = "2025-03-23 02:57:28" | |
| class AuthManager: | |
| def __init__(self): | |
| self.config = FIREBASE_CONFIG | |
| self.sessions = {} | |
| self.initialize_firebase() | |
| def initialize_firebase(self): | |
| try: | |
| if not firebase_admin._apps: | |
| cred = credentials.Certificate({ | |
| "type": "service_account", | |
| "project_id": self.config["projectId"], | |
| }) | |
| firebase_admin.initialize_app(cred) | |
| except Exception as e: | |
| print(f"Firebase initialization error: {e}") | |
| def generate_session_token(self): | |
| return secrets.token_urlsafe(32) | |
| def create_session(self, user_data): | |
| session_token = self.generate_session_token() | |
| self.sessions[session_token] = { | |
| 'user': user_data, | |
| 'created_at': datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S'), | |
| 'last_activity': datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S') | |
| } | |
| return session_token | |
| def verify_session(self, session_token): | |
| return session_token in self.sessions | |
| def get_user_data(self, session_token): | |
| return self.sessions.get(session_token, {}).get('user') | |
| def create_auth_interface(): | |
| auth_manager = AuthManager() | |
| with gr.Blocks() as auth_demo: | |
| gr.HTML(f""" | |
| <div style="text-align: center; background-color: #f0f2f6; padding: 20px; border-radius: 10px; margin-bottom: 20px;"> | |
| <h1 style="color: #2c3e50; font-size: 2.5em; margin-bottom: 10px;">π LegalEase Authentication</h1> | |
| <div style="display: flex; justify-content: center; gap: 40px; color: #576574; font-size: 1.1em;"> | |
| <div style="background-color: #ffffff; padding: 10px 20px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);"> | |
| <span style="font-weight: bold;">Current User:</span> {CURRENT_USER} | |
| </div> | |
| <div style="background-color: #ffffff; padding: 10px 20px; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1);"> | |
| <span style="font-weight: bold;">Last Updated:</span> {CURRENT_TIME} UTC | |
| </div> | |
| </div> | |
| </div> | |
| """) | |
| firebase_init = gr.HTML(""" | |
| <script src="https://www.gstatic.com/firebasejs/9.6.10/firebase-app-compat.js"></script> | |
| <script src="https://www.gstatic.com/firebasejs/9.6.10/firebase-auth-compat.js"></script> | |
| <script> | |
| const firebaseConfig = { | |
| apiKey: "AIzaSyA-HtFUWNpU6jEe4Cg6LQQSwartBtiqf1o", | |
| authDomain: "legalease-7cb84.firebaseapp.com", | |
| projectId: "legalease-7cb84", | |
| storageBucket: "legalease-7cb84.appspot.com", | |
| messagingSenderId: "734958314454", | |
| appId: "1:734958314454:web:43175e12ddcda05fc2fa77" | |
| }; | |
| firebase.initializeApp(firebaseConfig); | |
| function signInWithGoogle() { | |
| const provider = new firebase.auth.GoogleAuthProvider(); | |
| firebase.auth().signInWithPopup(provider) | |
| .then((result) => { | |
| const user = result.user; | |
| document.getElementById('login-status').innerHTML = | |
| `Successfully signed in as ${user.email}`; | |
| setTimeout(() => { | |
| window.location.reload(); | |
| }, 1000); | |
| }) | |
| .catch((error) => { | |
| document.getElementById('login-status').innerHTML = | |
| `Error: ${error.message}`; | |
| }); | |
| } | |
| </script> | |
| """) | |
| with gr.Tabs(): | |
| with gr.Tab("π Sign In"): | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| email_login = gr.Textbox( | |
| label="π§ Email", | |
| placeholder="Enter your email..." | |
| ) | |
| password_login = gr.Textbox( | |
| label="π Password", | |
| placeholder="Enter your password...", | |
| type="password" | |
| ) | |
| login_btn = gr.Button( | |
| "π Sign In", | |
| variant="primary" | |
| ) | |
| gr.HTML("<div style='height: 10px'></div>") | |
| google_sign_in_btn = gr.Button( | |
| "π Sign in with Google", | |
| variant="secondary" | |
| ) | |
| login_status = gr.HTML( | |
| '<div id="login-status"></div>' | |
| ) | |
| with gr.Column(scale=1): | |
| login_output = gr.Textbox( | |
| label="π Sign-in Status", | |
| lines=4, | |
| show_copy_button=True | |
| ) | |
| with gr.Tab("π Register"): | |
| with gr.Row(): | |
| with gr.Column(scale=1): | |
| email_register = gr.Textbox( | |
| label="π§ Email", | |
| placeholder="Enter your email..." | |
| ) | |
| password_register = gr.Textbox( | |
| label="π Password", | |
| placeholder="Enter your password...", | |
| type="password" | |
| ) | |
| confirm_password = gr.Textbox( | |
| label="π Confirm Password", | |
| placeholder="Confirm your password...", | |
| type="password" | |
| ) | |
| register_btn = gr.Button( | |
| "π Register", | |
| variant="primary" | |
| ) | |
| with gr.Column(scale=1): | |
| register_output = gr.Textbox( | |
| label="π Registration Status", | |
| lines=4, | |
| show_copy_button=True | |
| ) | |
| def login(email, password): | |
| try: | |
| if not email or not password: | |
| return "β Please enter both email and password." | |
| user_data = { | |
| 'email': email, | |
| 'name': email.split('@')[0], | |
| 'login_time': CURRENT_TIME | |
| } | |
| session_token = auth_manager.create_session(user_data) | |
| return f"β Login successful! Welcome {user_data['name']}!" | |
| except Exception as e: | |
| return f"β Login failed: {str(e)}" | |
| def register(email, password, confirm): | |
| try: | |
| if not email or not password: | |
| return "β Please enter both email and password." | |
| if password != confirm: | |
| return "β Passwords do not match!" | |
| if len(password) < 6: | |
| return "β Password must be at least 6 characters long!" | |
| return "β Registration successful! Please sign in." | |
| except Exception as e: | |
| return f"β Registration failed: {str(e)}" | |
| def google_sign_in(): | |
| return gr.HTML(""" | |
| <script> | |
| signInWithGoogle(); | |
| </script> | |
| <div>Initiating Google Sign-In...</div> | |
| """) | |
| login_btn.click( | |
| fn=login, | |
| inputs=[email_login, password_login], | |
| outputs=login_output | |
| ) | |
| register_btn.click( | |
| fn=register, | |
| inputs=[email_register, password_register, confirm_password], | |
| outputs=register_output | |
| ) | |
| google_sign_in_btn.click( | |
| fn=google_sign_in, | |
| inputs=[], | |
| outputs=login_status | |
| ) | |
| return auth_demo | |
| if __name__ == "__main__": | |
| demo = create_auth_interface() | |
| demo.launch() |