-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
77 lines (67 loc) · 2.17 KB
/
server.py
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from flask import Flask, request, jsonify
from flask_cors import CORS
import pyodbc
app = Flask(__name__)
CORS(app)
# Database configuration
DB_CONFIG = {
"server": r"PRADEEP\SQLEXPRESS", # Replace with your server name
"database": "Login_authentication", # Replace with your database name
"driver": "SQL SERVER",
}
# Connect to SQL Server
def get_db_connection():
conn = pyodbc.connect(
f"DRIVER={DB_CONFIG['driver']};"
f"SERVER={DB_CONFIG['server']};"
f"DATABASE={DB_CONFIG['database']};"
f"Trusted_Connection=yes;"
)
return conn
# Route: Sign-up
@app.route("/signup", methods=["POST"])
def signup():
data = request.json
username = data.get("username")
email = data.get("email")
password = data.get("password")
if not (username and email and password):
return jsonify({"error": "All fields are required"}), 400
try:
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute(
"INSERT INTO Users (Username, Email, Password) VALUES (?, ?, ?)",
(username, email, password), # Store hashed password in production!
)
conn.commit()
return jsonify({"message": "User registered successfully"}), 201
except pyodbc.IntegrityError:
return jsonify({"error": "Email already exists"}), 400
finally:
conn.close()
# Route: Sign-in
@app.route("/signin", methods=["POST"])
def signin():
data = request.json
username = data.get("username")
password = data.get("password")
if not (username and password):
return jsonify({"error": "Username and password are required"}), 400
try:
conn = get_db_connection()
cursor = conn.cursor()
cursor.execute(
"SELECT * FROM Users WHERE Username = ? AND Password = ?",
(username, password),
)
user = cursor.fetchone()
if user:
return jsonify({"message": "Sign-in successful!"}), 200
else:
return jsonify({"error": "Invalid username or password"}), 401
finally:
conn.close()
# Start the server
if __name__ == "__main__":
app.run(debug=True, port=5000)