Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 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 |
+
# Initialize SQLite
|
| 19 |
+
conn = sqlite3.connect("users.db", check_same_thread=False)
|
| 20 |
+
cursor = conn.cursor()
|
| 21 |
+
cursor.execute('''
|
| 22 |
+
CREATE TABLE IF NOT EXISTS users (
|
| 23 |
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 24 |
+
email TEXT UNIQUE NOT NULL,
|
| 25 |
+
password TEXT NOT NULL
|
| 26 |
+
)
|
| 27 |
+
''')
|
| 28 |
+
conn.commit()
|
| 29 |
+
|
| 30 |
+
# Utility function to hash passwords
|
| 31 |
+
def hash_password(password: str) -> str:
|
| 32 |
+
return hashlib.sha256(password.encode()).hexdigest()
|
| 33 |
+
|
| 34 |
+
# Models
|
| 35 |
+
class SignupForm(BaseModel):
|
| 36 |
+
email: str
|
| 37 |
+
password: str
|
| 38 |
+
|
| 39 |
+
class LoginForm(BaseModel):
|
| 40 |
+
email: str
|
| 41 |
+
password: str
|
| 42 |
+
|
| 43 |
+
# Routes
|
| 44 |
+
@app.post("/signup")
|
| 45 |
+
def signup(data: SignupForm):
|
| 46 |
+
email = data.email.lower().strip()
|
| 47 |
+
password = hash_password(data.password)
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
cursor.execute("INSERT INTO users (email, password) VALUES (?, ?)", (email, password))
|
| 51 |
+
conn.commit()
|
| 52 |
+
return {"success": True, "message": "Account created successfully"}
|
| 53 |
+
except sqlite3.IntegrityError:
|
| 54 |
+
raise HTTPException(status_code=409, detail="Email already registered")
|
| 55 |
+
|
| 56 |
+
@app.post("/login")
|
| 57 |
+
def login(data: LoginForm):
|
| 58 |
+
email = data.email.lower().strip()
|
| 59 |
+
password = hash_password(data.password)
|
| 60 |
+
|
| 61 |
+
cursor.execute("SELECT * FROM users WHERE email = ? AND password = ?", (email, password))
|
| 62 |
+
user = cursor.fetchone()
|
| 63 |
+
|
| 64 |
+
if user:
|
| 65 |
+
return {"success": True, "message": "Login successful"}
|
| 66 |
+
else:
|
| 67 |
+
raise HTTPException(status_code=401, detail="Invalid credentials")
|
| 68 |
+
|
| 69 |
+
@app.get("/")
|
| 70 |
+
def root():
|
| 71 |
+
return {"message": "FastAPI Auth API is running 🚀"}
|