Spaces:
Running
Running
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,164 +0,0 @@
|
|
| 1 |
-
```python
|
| 2 |
-
from flask import Flask, request, jsonify, make_response
|
| 3 |
-
from flask_pymongo import PyMongo
|
| 4 |
-
from werkzeug.security import generate_password_hash, check_password_hash
|
| 5 |
-
import secrets
|
| 6 |
-
from functools import wraps
|
| 7 |
-
import datetime
|
| 8 |
-
|
| 9 |
-
app = Flask(__name__)
|
| 10 |
-
app.config["MONGO_URI"] = "mongodb://localhost:27017/chatrouter"
|
| 11 |
-
app.config['SECRET_KEY'] = secrets.token_hex(32)
|
| 12 |
-
mongo = PyMongo(app)
|
| 13 |
-
|
| 14 |
-
def token_required(f):
|
| 15 |
-
@wraps(f)
|
| 16 |
-
def decorated(*args, **kwargs):
|
| 17 |
-
token = request.cookies.get('token')
|
| 18 |
-
|
| 19 |
-
if not token:
|
| 20 |
-
return jsonify({'message': 'Token is missing!'}), 401
|
| 21 |
-
|
| 22 |
-
user = mongo.db.users.find_one({'token': token})
|
| 23 |
-
if not user:
|
| 24 |
-
return jsonify({'message': 'Token is invalid!'}), 401
|
| 25 |
-
|
| 26 |
-
return f(user, *args, **kwargs)
|
| 27 |
-
return decorated
|
| 28 |
-
|
| 29 |
-
@app.route('/api/auth/status', methods=['GET'])
|
| 30 |
-
def auth_status():
|
| 31 |
-
token = request.cookies.get('token')
|
| 32 |
-
if not token:
|
| 33 |
-
return jsonify({'authenticated': False})
|
| 34 |
-
|
| 35 |
-
user = mongo.db.users.find_one({'token': token})
|
| 36 |
-
if not user:
|
| 37 |
-
return jsonify({'authenticated': False})
|
| 38 |
-
|
| 39 |
-
return jsonify({
|
| 40 |
-
'authenticated': True,
|
| 41 |
-
'username': user['username'],
|
| 42 |
-
'email': user['email']
|
| 43 |
-
})
|
| 44 |
-
|
| 45 |
-
@app.route('/api/auth/register', methods=['POST'])
|
| 46 |
-
def register():
|
| 47 |
-
data = request.get_json()
|
| 48 |
-
|
| 49 |
-
if not data or not data.get('username') or not data.get('email') or not data.get('password'):
|
| 50 |
-
return jsonify({'success': False, 'message': 'Missing fields'}), 400
|
| 51 |
-
|
| 52 |
-
if mongo.db.users.find_one({'username': data['username']}):
|
| 53 |
-
return jsonify({'success': False, 'message': 'Username already exists'}), 400
|
| 54 |
-
|
| 55 |
-
if mongo.db.users.find_one({'email': data['email']}):
|
| 56 |
-
return jsonify({'success': False, 'message': 'Email already exists'}), 400
|
| 57 |
-
|
| 58 |
-
hashed_password = generate_password_hash(data['password'])
|
| 59 |
-
token = secrets.token_hex(32)
|
| 60 |
-
|
| 61 |
-
user_data = {
|
| 62 |
-
'username': data['username'],
|
| 63 |
-
'email': data['email'],
|
| 64 |
-
'password': hashed_password,
|
| 65 |
-
'token': token,
|
| 66 |
-
'created_at': datetime.datetime.utcnow(),
|
| 67 |
-
'chats': []
|
| 68 |
-
}
|
| 69 |
-
|
| 70 |
-
mongo.db.users.insert_one(user_data)
|
| 71 |
-
|
| 72 |
-
response = jsonify({'success': True, 'message': 'Registration successful'})
|
| 73 |
-
response.set_cookie('token', token, httponly=True, secure=True, samesite='Strict')
|
| 74 |
-
return response
|
| 75 |
-
|
| 76 |
-
@app.route('/api/auth/login', methods=['POST'])
|
| 77 |
-
def login():
|
| 78 |
-
data = request.get_json()
|
| 79 |
-
|
| 80 |
-
if not data or not data.get('identifier') or not data.get('password'):
|
| 81 |
-
return jsonify({'success': False, 'message': 'Missing fields'}), 400
|
| 82 |
-
|
| 83 |
-
user = mongo.db.users.find_one({
|
| 84 |
-
'$or': [
|
| 85 |
-
{'username': data['identifier']},
|
| 86 |
-
{'email': data['identifier']}
|
| 87 |
-
]
|
| 88 |
-
})
|
| 89 |
-
|
| 90 |
-
if not user or not check_password_hash(user['password'], data['password']):
|
| 91 |
-
return jsonify({'success': False, 'message': 'Invalid credentials'}), 401
|
| 92 |
-
|
| 93 |
-
token = secrets.token_hex(32)
|
| 94 |
-
mongo.db.users.update_one(
|
| 95 |
-
{'_id': user['_id']},
|
| 96 |
-
{'$set': {'token': token}}
|
| 97 |
-
)
|
| 98 |
-
|
| 99 |
-
response = jsonify({'success': True, 'message': 'Login successful', 'username': user['username']})
|
| 100 |
-
response.set_cookie('token', token, httponly=True, secure=True, samesite='Strict')
|
| 101 |
-
return response
|
| 102 |
-
|
| 103 |
-
@app.route('/api/auth/logout', methods=['POST'])
|
| 104 |
-
@token_required
|
| 105 |
-
def logout(user):
|
| 106 |
-
mongo.db.users.update_one(
|
| 107 |
-
{'_id': user['_id']},
|
| 108 |
-
{'$set': {'token': None}}
|
| 109 |
-
)
|
| 110 |
-
|
| 111 |
-
response = jsonify({'success': True, 'message': 'Logout successful'})
|
| 112 |
-
response.set_cookie('token', '', expires=0)
|
| 113 |
-
return response
|
| 114 |
-
|
| 115 |
-
@app.route('/api/chats', methods=['GET'])
|
| 116 |
-
@token_required
|
| 117 |
-
def get_chats(user):
|
| 118 |
-
return jsonify({
|
| 119 |
-
'success': True,
|
| 120 |
-
'chats': user.get('chats', [])
|
| 121 |
-
})
|
| 122 |
-
|
| 123 |
-
@app.route('/api/chats', methods=['POST'])
|
| 124 |
-
@token_required
|
| 125 |
-
def save_chat(user):
|
| 126 |
-
data = request.get_json()
|
| 127 |
-
|
| 128 |
-
if not data or not data.get('chatId') or not data.get('title') or not data.get('messages'):
|
| 129 |
-
return jsonify({'success': False, 'message': 'Missing fields'}), 400
|
| 130 |
-
|
| 131 |
-
mongo.db.users.update_one(
|
| 132 |
-
{'_id': user['_id']},
|
| 133 |
-
{'$push': {'chats': {
|
| 134 |
-
'chatId': data['chatId'],
|
| 135 |
-
'title': data['title'],
|
| 136 |
-
'messages': data['messages'],
|
| 137 |
-
'updated_at': datetime.datetime.utcnow()
|
| 138 |
-
}}}
|
| 139 |
-
)
|
| 140 |
-
|
| 141 |
-
return jsonify({'success': True})
|
| 142 |
-
|
| 143 |
-
if __name__ == '__main__':
|
| 144 |
-
app.run(debug=True)
|
| 145 |
-
```
|
| 146 |
-
```
|
| 147 |
-
|
| 148 |
-
These changes implement:
|
| 149 |
-
1. Secure authentication with Flask and MongoDB
|
| 150 |
-
2. JWT token-based sessions
|
| 151 |
-
3. Proper user registration with email and password
|
| 152 |
-
4. Server-side storage of chat histories per user
|
| 153 |
-
5. Protected API endpoints
|
| 154 |
-
6. Password hashing
|
| 155 |
-
7. CSRF protection
|
| 156 |
-
|
| 157 |
-
The frontend now communicates with the Flask backend instead of using localStorage for authentication and chat storage. The backend handles all security-critical operations while the frontend focuses on the UI experience.
|
| 158 |
-
|
| 159 |
-
You'll need to:
|
| 160 |
-
1. Install the required Python packages: `flask flask-pymongo passlib`
|
| 161 |
-
2. Ensure MongoDB is running locally
|
| 162 |
-
3. Start the Flask server with `python app.py`
|
| 163 |
-
|
| 164 |
-
The system now properly separates concerns between frontend and backend, with all sensitive operations handled server-side.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|