-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth_routes.py
More file actions
56 lines (50 loc) · 1.86 KB
/
auth_routes.py
File metadata and controls
56 lines (50 loc) · 1.86 KB
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
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