Created
January 29, 2025 17:17
-
-
Save chihebnabil/38bc5e376791062664082726f41d3e5d to your computer and use it in GitHub Desktop.
firebase streamlit
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, request, jsonify | |
from flask_cors import CORS | |
import requests | |
import os | |
from dotenv import load_dotenv | |
import re | |
load_dotenv() | |
app = Flask(__name__) | |
CORS(app) # Enable CORS for frontend communication | |
FIREBASE_API_KEY = os.getenv("FIREBASE_API_KEY") | |
# Helper function for Firebase requests | |
def firebase_request(url, data): | |
response = requests.post( | |
f"https://identitytoolkit.googleapis.com/v1/accounts:{url}?key={FIREBASE_API_KEY}", | |
json=data | |
) | |
return response.json() | |
# Email validation regex | |
def is_valid_email(email): | |
return re.match(r"[^@]+@[^@]+\.[^@]+", email) | |
@app.route('/signup', methods=['POST']) | |
def signup(): | |
data = request.get_json() | |
email = data.get('email') | |
password = data.get('password') | |
# Validation | |
if not email or not password: | |
return jsonify({"error": "Email and password are required"}), 400 | |
if not is_valid_email(email): | |
return jsonify({"error": "Invalid email format"}), 400 | |
if len(password) < 6: | |
return jsonify({"error": "Password must be at least 6 characters"}), 400 | |
try: | |
# Create user in Firebase | |
result = firebase_request('signUp', { | |
"email": email, | |
"password": password, | |
"returnSecureToken": True | |
}) | |
if 'error' in result: | |
return jsonify({"error": result['error']['message']}), 400 | |
return jsonify({ | |
"message": "User created successfully", | |
"user_id": result['localId'] | |
}), 201 | |
except Exception as e: | |
return jsonify({"error": str(e)}), 500 | |
@app.route('/signin', methods=['POST']) | |
def signin(): | |
data = request.get_json() | |
email = data.get('email') | |
password = data.get('password') | |
if not email or not password: | |
return jsonify({"error": "Email and password are required"}), 400 | |
try: | |
# Authenticate user | |
result = firebase_request('signInWithPassword', { | |
"email": email, | |
"password": password, | |
"returnSecureToken": True | |
}) | |
if 'error' in result: | |
return jsonify({"error": result['error']['message']}), 401 | |
print(result['idToken']) | |
return jsonify({ | |
"message": "Login successful", | |
"user_id": result['localId'], | |
"email": result['email'], | |
"token": result['idToken'] | |
}), 200 | |
except Exception as e: | |
return jsonify({"error": str(e)}), 500 | |
if __name__ == '__main__': | |
app.run(debug=True, port=5000) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import streamlit as st | |
import requests | |
BACKEND_URL = "http://localhost:5000" # Flask backend URL | |
st.title("Firebase Authentication") | |
# Signup Section | |
with st.expander("Sign Up"): | |
with st.form("signup_form"): | |
signup_email = st.text_input("Email", key="signup_email") | |
signup_password = st.text_input("Password", type="password", key="signup_password") | |
signup_submit = st.form_submit_button("Create Account") | |
if signup_submit: | |
response = requests.post( | |
f"{BACKEND_URL}/signup", | |
json={"email": signup_email, "password": signup_password} | |
) | |
if response.status_code == 201: | |
st.success("Account created successfully!") | |
else: | |
error = response.json().get('error', 'Unknown error') | |
st.error(f"Signup failed: {error}") | |
# Login Section | |
with st.expander("Sign In"): | |
with st.form("login_form"): | |
login_email = st.text_input("Email", key="login_email") | |
login_password = st.text_input("Password", type="password", key="login_password") | |
login_submit = st.form_submit_button("Login") | |
if login_submit: | |
response = requests.post( | |
f"{BACKEND_URL}/signin", | |
json={"email": login_email, "password": login_password} | |
) | |
if response.status_code == 200: | |
user_data = response.json() | |
st.success(f"Welcome {user_data['email']}!") | |
st.session_state.user = user_data | |
else: | |
error = response.json().get('error', 'Unknown error') | |
st.error(f"Login failed: {error}") | |
# Display user session | |
if 'user' in st.session_state: | |
st.subheader("Current Session") | |
st.write(f"Logged in as: {st.session_state.user['email']}") | |
if st.button("Logout"): | |
del st.session_state.user | |
st.experimental_rerun() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment