Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions backend/auth_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from flask import Blueprint, request, jsonify
from supabase import create_client, Client
from dotenv import load_dotenv
from functools import wraps
import os

load_dotenv()

supabase_url = os.getenv("SUPABASE_URL")
supabase_key = os.getenv("SUPABASE_KEY")
supabase: Client = create_client(supabase_url, supabase_key)

auth = Blueprint("auth", __name__)

# Function to verify JWT token
def verify_token(token):
try:
user = supabase.auth.get_user(token)
return user
except Exception:
return None

# Sign-up route
@auth.route("/signup", methods=["POST"])
def signup():
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:
user = supabase.auth.sign_up({"email": email, "password": password})
return jsonify({"message": "User created successfully"}), 201
except Exception as e:
return jsonify({"error": str(e)}), 400

# Sign-in route
@auth.route("/signin", methods=["POST"])
def signin():
# Sign in a user with email and password, returning access and refresh tokens.
data = request.get_json()
email = data.get("email")
password = data.get("password")
if not email or not password:
return jsonify({"status": "error", "message": "Email and password are required"}), 400
try:
response = supabase.auth.sign_in_with_password({"email": email, "password": password})
if response.session is None:
return jsonify({"status": "error", "message": "Authentication failed"}), 401
return jsonify({
"status": "success",
"access_token": response.session.access_token,
"refresh_token": response.session.refresh_token
}), 200
except Exception as e:
return jsonify({"error": str(e)}), 400
3 changes: 3 additions & 0 deletions backend/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
from flask_cors import CORS
from dotenv import load_dotenv
from openai import OpenAI
from auth_routes import auth

load_dotenv()

app = Flask(__name__)
CORS(app)

app.register_blueprint(auth)

# Create the OpenAI client
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))

Expand Down
3 changes: 2 additions & 1 deletion backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
flask
flask-cors
openai
python-dotenv
python-dotenv
supabase