Ali2206 commited on
Commit
3d0dcf7
·
verified ·
1 Parent(s): 667d88e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -8
app.py CHANGED
@@ -1,25 +1,31 @@
1
- from fastapi import FastAPI, HTTPException, Form
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from pydantic import BaseModel
4
  import sqlite3
5
  import hashlib
6
  import os
 
7
  app = FastAPI()
8
 
9
- # CORS (allow frontend on another origin like mobile app)
10
  app.add_middleware(
11
  CORSMiddleware,
12
- allow_origins=["*"], # or set your domain/app origin
13
  allow_credentials=True,
14
  allow_methods=["*"],
15
  allow_headers=["*"],
16
  )
17
 
 
 
 
 
18
 
19
- db_path = os.path.join(os.getenv("HF_HOME", "/data"), "users.db")
20
  conn = sqlite3.connect(db_path, check_same_thread=False)
21
-
22
  cursor = conn.cursor()
 
 
23
  cursor.execute('''
24
  CREATE TABLE IF NOT EXISTS users (
25
  id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -29,11 +35,11 @@ cursor.execute('''
29
  ''')
30
  conn.commit()
31
 
32
- # Utility function to hash passwords
33
  def hash_password(password: str) -> str:
34
  return hashlib.sha256(password.encode()).hexdigest()
35
 
36
- # Models
37
  class SignupForm(BaseModel):
38
  email: str
39
  password: str
@@ -42,7 +48,7 @@ class LoginForm(BaseModel):
42
  email: str
43
  password: str
44
 
45
- # Routes
46
  @app.post("/signup")
47
  def signup(data: SignupForm):
48
  email = data.email.lower().strip()
@@ -55,6 +61,7 @@ def signup(data: SignupForm):
55
  except sqlite3.IntegrityError:
56
  raise HTTPException(status_code=409, detail="Email already registered")
57
 
 
58
  @app.post("/login")
59
  def login(data: LoginForm):
60
  email = data.email.lower().strip()
@@ -68,6 +75,7 @@ def login(data: LoginForm):
68
  else:
69
  raise HTTPException(status_code=401, detail="Invalid credentials")
70
 
 
71
  @app.get("/")
72
  def root():
73
  return {"message": "FastAPI Auth API is running 🚀"}
 
1
+ from fastapi import FastAPI, HTTPException
2
  from fastapi.middleware.cors import CORSMiddleware
3
  from pydantic import BaseModel
4
  import sqlite3
5
  import hashlib
6
  import os
7
+
8
  app = FastAPI()
9
 
10
+ # Allow all CORS origins (change this if needed for production)
11
  app.add_middleware(
12
  CORSMiddleware,
13
+ allow_origins=["*"],
14
  allow_credentials=True,
15
  allow_methods=["*"],
16
  allow_headers=["*"],
17
  )
18
 
19
+ # ✅ Ensure DB path is in a writable directory for Hugging Face Spaces
20
+ HF_DATA_DIR = os.getenv("HF_HOME", "/data")
21
+ os.makedirs(HF_DATA_DIR, exist_ok=True)
22
+ db_path = os.path.join(HF_DATA_DIR, "users.db")
23
 
24
+ # Connect to the database
25
  conn = sqlite3.connect(db_path, check_same_thread=False)
 
26
  cursor = conn.cursor()
27
+
28
+ # Create users table if it doesn't exist
29
  cursor.execute('''
30
  CREATE TABLE IF NOT EXISTS users (
31
  id INTEGER PRIMARY KEY AUTOINCREMENT,
 
35
  ''')
36
  conn.commit()
37
 
38
+ # Utility function to hash passwords securely
39
  def hash_password(password: str) -> str:
40
  return hashlib.sha256(password.encode()).hexdigest()
41
 
42
+ # Request models
43
  class SignupForm(BaseModel):
44
  email: str
45
  password: str
 
48
  email: str
49
  password: str
50
 
51
+ # Route to sign up new users
52
  @app.post("/signup")
53
  def signup(data: SignupForm):
54
  email = data.email.lower().strip()
 
61
  except sqlite3.IntegrityError:
62
  raise HTTPException(status_code=409, detail="Email already registered")
63
 
64
+ # Route to log in existing users
65
  @app.post("/login")
66
  def login(data: LoginForm):
67
  email = data.email.lower().strip()
 
75
  else:
76
  raise HTTPException(status_code=401, detail="Invalid credentials")
77
 
78
+ # Test root route
79
  @app.get("/")
80
  def root():
81
  return {"message": "FastAPI Auth API is running 🚀"}