Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -446,9 +446,49 @@ def login():
|
|
446 |
|
447 |
@app.route("/logout")
|
448 |
def logout():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
449 |
session.clear()
|
450 |
flash("You have been logged out.")
|
451 |
return redirect(url_for('login'))
|
|
|
452 |
############## END LOGIN #############################################
|
453 |
|
454 |
@app.route('/get_response', methods=['POST'])
|
|
|
446 |
|
447 |
@app.route("/logout")
|
448 |
def logout():
|
449 |
+
# Fetch the current device info
|
450 |
+
user_agent = request.headers.get('User-Agent')
|
451 |
+
|
452 |
+
# Load the users from the user.json file
|
453 |
+
url = f"https://api.github.com/repos/omarnuwrar/{repository_name}/contents/user.json"
|
454 |
+
headers = {
|
455 |
+
"Authorization": f"token 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", # Replace with your GitHub token
|
456 |
+
"Accept": "application/vnd.github.v3+json"
|
457 |
+
}
|
458 |
+
response = requests.get(url, headers=headers)
|
459 |
+
if response.status_code == 200:
|
460 |
+
file_info = response.json()
|
461 |
+
file_content = base64.b64decode(file_info['content']).decode()
|
462 |
+
users = eval(file_content) # Convert the string to a list of dictionaries
|
463 |
+
|
464 |
+
# Check if the current device info is associated with any user
|
465 |
+
for user in users:
|
466 |
+
if 'device_info' in user and user['device_info'] == user_agent:
|
467 |
+
# Remove the device info
|
468 |
+
del user['device_info']
|
469 |
+
|
470 |
+
# Convert users list back to JSON string
|
471 |
+
updated_file_content = json.dumps(users, indent=4)
|
472 |
+
|
473 |
+
# Update the user.json file in the repository
|
474 |
+
update_data = {
|
475 |
+
"message": "Remove device info on logout",
|
476 |
+
"content": base64.b64encode(updated_file_content.encode()).decode(),
|
477 |
+
"sha": file_info['sha']
|
478 |
+
}
|
479 |
+
update_response = requests.put(url, headers=headers, json=update_data)
|
480 |
+
if update_response.status_code == 200:
|
481 |
+
print("Device info removed on logout.")
|
482 |
+
else:
|
483 |
+
print("Failed to update user info.")
|
484 |
+
|
485 |
+
break # Exit loop after processing
|
486 |
+
|
487 |
+
# Clear the session and log the user out
|
488 |
session.clear()
|
489 |
flash("You have been logged out.")
|
490 |
return redirect(url_for('login'))
|
491 |
+
|
492 |
############## END LOGIN #############################################
|
493 |
|
494 |
@app.route('/get_response', methods=['POST'])
|