Spaces:
Sleeping
Sleeping
Update routes.py
Browse files
routes.py
CHANGED
@@ -60,20 +60,33 @@ def register():
|
|
60 |
|
61 |
@app.route('/chat')
|
62 |
def chat():
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
session.clear()
|
69 |
return redirect(url_for('landing'))
|
70 |
-
|
71 |
-
# Update user online status
|
72 |
-
user.online = True
|
73 |
-
user.last_seen = datetime.utcnow()
|
74 |
-
db.session.commit()
|
75 |
-
|
76 |
-
return render_template('chat.html', user=user)
|
77 |
|
78 |
@app.route('/settings')
|
79 |
def settings():
|
@@ -101,6 +114,12 @@ def logout():
|
|
101 |
|
102 |
# API Routes
|
103 |
|
|
|
|
|
|
|
|
|
|
|
|
|
104 |
@app.route('/api/register', methods=['POST'])
|
105 |
def api_register():
|
106 |
try:
|
@@ -124,14 +143,25 @@ def api_register():
|
|
124 |
session.clear() # Clear any existing session
|
125 |
session['user_id'] = user.user_id
|
126 |
session.permanent = True # Make the session permanent
|
|
|
127 |
|
128 |
-
|
|
|
129 |
|
130 |
-
|
|
|
|
|
|
|
131 |
'success': True,
|
132 |
'message': 'Account created successfully',
|
133 |
-
'redirect': url_for('chat')
|
|
|
134 |
})
|
|
|
|
|
|
|
|
|
|
|
135 |
|
136 |
except Exception as e:
|
137 |
app.logger.error(f"Registration error: {e}")
|
|
|
60 |
|
61 |
@app.route('/chat')
|
62 |
def chat():
|
63 |
+
try:
|
64 |
+
user_id = session.get('user_id')
|
65 |
+
if not user_id:
|
66 |
+
app.logger.warning("No user_id in session, redirecting to landing")
|
67 |
+
return redirect(url_for('landing'))
|
68 |
+
|
69 |
+
user = User.query.get(user_id)
|
70 |
+
if not user:
|
71 |
+
app.logger.warning(f"User {user_id} not found in database, clearing session")
|
72 |
+
session.clear()
|
73 |
+
return redirect(url_for('landing'))
|
74 |
+
|
75 |
+
# Update user online status
|
76 |
+
user.online = True
|
77 |
+
user.last_seen = datetime.utcnow()
|
78 |
+
db.session.commit()
|
79 |
+
|
80 |
+
# Ensure session is still valid after database operations
|
81 |
+
session.modified = True
|
82 |
+
|
83 |
+
app.logger.info(f"Rendering chat for user {user_id}")
|
84 |
+
return render_template('chat.html', user=user)
|
85 |
+
|
86 |
+
except Exception as e:
|
87 |
+
app.logger.error(f"Error in chat route: {e}")
|
88 |
session.clear()
|
89 |
return redirect(url_for('landing'))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
90 |
|
91 |
@app.route('/settings')
|
92 |
def settings():
|
|
|
114 |
|
115 |
# API Routes
|
116 |
|
117 |
+
@app.route('/api/check_session')
|
118 |
+
def check_session():
|
119 |
+
if 'user_id' in session:
|
120 |
+
return jsonify({'success': True, 'user_id': session['user_id']})
|
121 |
+
return jsonify({'success': False, 'message': 'No active session'}), 401
|
122 |
+
|
123 |
@app.route('/api/register', methods=['POST'])
|
124 |
def api_register():
|
125 |
try:
|
|
|
143 |
session.clear() # Clear any existing session
|
144 |
session['user_id'] = user.user_id
|
145 |
session.permanent = True # Make the session permanent
|
146 |
+
session.modified = True # Ensure the session is saved
|
147 |
|
148 |
+
# Force a database commit to ensure user is saved
|
149 |
+
db.session.commit()
|
150 |
|
151 |
+
app.logger.info(f"User registered successfully: {user.user_id}, Session: {session}")
|
152 |
+
|
153 |
+
# Create response
|
154 |
+
response = jsonify({
|
155 |
'success': True,
|
156 |
'message': 'Account created successfully',
|
157 |
+
'redirect': url_for('chat'),
|
158 |
+
'user_id': user.user_id
|
159 |
})
|
160 |
+
|
161 |
+
# Set a cookie to help with session persistence
|
162 |
+
response.set_cookie('user_registered', 'true', max_age=60)
|
163 |
+
|
164 |
+
return response
|
165 |
|
166 |
except Exception as e:
|
167 |
app.logger.error(f"Registration error: {e}")
|