-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdash.py
More file actions
335 lines (279 loc) · 11.7 KB
/
dash.py
File metadata and controls
335 lines (279 loc) · 11.7 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
import csv
import os
from functools import wraps
from flask import Flask, jsonify, request, session
from flask_cors import CORS
from werkzeug.security import check_password_hash, generate_password_hash
app = Flask(__name__)
app.secret_key = "your_secret_key" # Set a strong secret key here
# Enable CORS properly
CORS(
app,
resources={
r"/*": {
"origins": ["http://127.0.0.1:5500", "http://localhost:5500"],
"supports_credentials": True,
"allow_headers": ["Content-Type", "Authorization"],
"methods": ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
}
},
)
# File to store user data
USERS_FILE = "users.csv"
# Create users file if it doesn't exist
def initialize_users_file():
if not os.path.exists(USERS_FILE):
with open(USERS_FILE, "w", newline="") as file:
writer = csv.writer(file)
writer.writerow(["username", "password"])
# Add default admin user
writer.writerow(["admin", generate_password_hash("1234")])
# Login required decorator
def login_required(f):
@wraps(f)
def decorated_function(*args, **kwargs):
if "username" not in session:
return (
jsonify({"success": False, "message": "Authentication required"}),
401,
)
return f(*args, **kwargs)
return decorated_function
# Routes
@app.route("/")
@login_required
def index():
return jsonify({"success": True, "message": "Welcome, you are logged in"})
@app.route("/login", methods=["GET", "POST", "OPTIONS"])
def login():
# Handle preflight OPTIONS request
if request.method == "OPTIONS":
return "", 200
if request.method == "POST":
# Process form data - try multiple ways to get the data
data = None
# Check if data is in form
if request.form:
username = request.form.get("username")
password = request.form.get("password")
# Check if data is in JSON
elif request.is_json:
data = request.get_json()
username = data.get("username")
password = data.get("password")
# Check raw data
else:
try:
# Try to parse the raw data
data = request.get_data()
print(f"Raw data received: {data}")
# Handle form data from fetch API
from urllib.parse import parse_qs
parsed_data = parse_qs(data.decode("utf-8"))
print(f"Parsed data: {parsed_data}")
username = parsed_data.get("username", [""])[0]
password = parsed_data.get("password", [""])[0]
except Exception as e:
print(f"Error parsing data: {str(e)}")
username = None
password = None
# Print debug info
print(f"Request content type: {request.content_type}")
print(f"Username received: {username}")
print(f"Password received: {'*' * len(password) if password else None}")
if not username or not password:
return (
jsonify(
{"success": False, "message": "Username and password are required"}
),
400,
)
# Check if user exists - with safer file handling
try:
# First check if file exists
if not os.path.exists(USERS_FILE):
print("Users file does not exist!")
initialize_users_file()
return (
jsonify(
{"success": False, "message": "Invalid username or password"}
),
401,
)
# Debug - print file contents
with open(USERS_FILE, "r", newline="") as debug_file:
content = debug_file.read()
print(
f"CSV file content preview: {content[:100]}..."
) # First 100 chars
# Now try to read it
found_user = False
with open(USERS_FILE, "r", newline="") as file:
reader = csv.reader(file)
for row in reader:
# Skip empty rows
if not row or len(row) < 2:
continue
# Skip header row if it exists
if row[0] == "username" and row[1] == "password":
continue
stored_username = row[0]
stored_password = row[1]
# Debug
print(f"Checking user: {stored_username}")
if stored_username == username:
found_user = True
try:
if check_password_hash(stored_password, password):
session["username"] = username
return jsonify(
{
"success": True,
"message": "Login successful!",
"redirect": "/search",
}
)
except Exception as e:
print(f"Password verification error: {str(e)}")
# If the password hash is invalid, continue to the invalid credentials message
if not found_user:
print(f"User {username} not found")
else:
print(f"User found but password didn't match")
return (
jsonify({"success": False, "message": "Invalid username or password"}),
401,
)
except Exception as e:
print(f"Login error details: {str(e)}")
import traceback
print(traceback.format_exc())
return jsonify({"success": False, "message": "Server error occurred"}), 500
# GET request
return jsonify({"success": True, "message": "Please login"})
@app.route("/signup", methods=["GET", "POST", "OPTIONS"])
def signup():
# Handle preflight OPTIONS request
if request.method == "OPTIONS":
return "", 200
if request.method == "POST":
try:
# Process form data - try multiple ways to get the data
if request.form:
username = request.form.get("username")
password = request.form.get("password")
confirm_password = request.form.get("confirm_password")
elif request.is_json:
data = request.get_json()
username = data.get("username")
password = data.get("password")
confirm_password = data.get("confirm_password")
else:
try:
data = request.get_data()
from urllib.parse import parse_qs
parsed_data = parse_qs(data.decode("utf-8"))
username = parsed_data.get("username", [""])[0]
password = parsed_data.get("password", [""])[0]
confirm_password = parsed_data.get("confirm_password", [""])[0]
except Exception as e:
print(f"Error parsing signup data: {str(e)}")
username = None
password = None
confirm_password = None
print(f"Signup request - Username: {username}")
if not username or not password or not confirm_password:
return (
jsonify({"success": False, "message": "All fields are required"}),
400,
)
# Validate input
if password != confirm_password:
return jsonify({"success": False, "message": "Passwords do not match"})
# Initialize CSV file if it doesn't exist
if not os.path.exists(USERS_FILE):
initialize_users_file()
# Check if username already exists - safer file handling
username_exists = False
try:
with open(USERS_FILE, "r", newline="") as file:
reader = csv.reader(file)
for row in reader:
# Skip the header if it exists
if row and row[0] == "username" and row[1] == "password":
continue
if row and row[0] == username:
username_exists = True
break
except Exception as e:
print(f"Error checking existing username: {str(e)}")
return jsonify({"success": False, "message": "Database error"}), 500
if username_exists:
return jsonify({"success": False, "message": "Username already exists"})
# Add new user
try:
# Check file structure before appending
file_has_header = False
if os.path.exists(USERS_FILE) and os.path.getsize(USERS_FILE) > 0:
with open(USERS_FILE, "r", newline="") as file:
reader = csv.reader(file)
first_row = next(reader, None)
if (
first_row
and first_row[0] == "username"
and first_row[1] == "password"
):
file_has_header = True
# If file doesn't have header, we need to create a new file
if not file_has_header:
initialize_users_file()
# Now append the new user
with open(USERS_FILE, "a", newline="") as file:
writer = csv.writer(file)
writer.writerow([username, generate_password_hash(password)])
print(f"Successfully added user: {username}")
return jsonify(
{
"success": True,
"message": "Sign-up successful! Please log in.",
"redirect": "/login",
}
)
except Exception as e:
print(f"Error adding new user: {str(e)}")
return (
jsonify({"success": False, "message": "Could not create account"}),
500,
)
except Exception as e:
print(f"Signup error details: {str(e)}")
import traceback
print(traceback.format_exc())
return jsonify({"success": False, "message": "Server error occurred"}), 500
return jsonify({"success": True, "message": "Please sign up"})
@app.route("/logout")
def logout():
session.pop("username", None)
return jsonify(
{"success": True, "message": "You have been logged out", "redirect": "/login"}
)
@app.route("/search")
@login_required
def search():
return jsonify(
{"success": True, "message": "Search page", "username": session.get("username")}
)
@app.after_request
def after_request(response):
# Set CORS headers
response.headers.add(
"Access-Control-Allow-Origin",
request.headers.get("Origin", "http://127.0.0.1:5500"),
)
response.headers.add("Access-Control-Allow-Headers", "Content-Type,Authorization")
response.headers.add("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS")
response.headers.add("Access-Control-Allow-Credentials", "true")
response.headers.add("Vary", "Origin") # Important for proper caching with CORS
return response
if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0")