@@ -41,6 +41,14 @@ def unauthorized():
4141 def load_user (user_id ):
4242 return User .query .get (int (user_id ))
4343
44+ # NOW we can use @app decorators - app exists here!
45+ @app .before_request
46+ def check_session ():
47+ """Ensure session is valid for API routes"""
48+ if request .path .startswith (('/api/' , '/settings/api/' , '/admin/api/' )):
49+ if not current_user .is_authenticated :
50+ return jsonify ({'error' : 'Authentication required' , 'redirect' : '/login' }), 401
51+
4452 # Error handlers for JSON responses
4553 @app .errorhandler (404 )
4654 def not_found (error ):
@@ -90,6 +98,8 @@ def index():
9098 def dashboard ():
9199 # Check if user has configured DirectAdmin settings
92100 if not current_user .has_da_config ():
101+ # Import flash here to avoid circular imports
102+ from flask import flash
93103 flash ('Please configure your DirectAdmin settings first.' , 'warning' )
94104 return redirect (url_for ('settings.index' ))
95105
@@ -180,15 +190,7 @@ def delete_forwarder(alias):
180190 print (f"Error deleting forwarder: { e } " )
181191 return jsonify ({'error' : str (e )}), 500
182192
183- # Add this after creating the app
184- @app .before_request
185- def check_session ():
186- """Ensure session is valid for API routes"""
187- if request .path .startswith (('/api/' , '/settings/api/' , '/admin/api/' )):
188- if not current_user .is_authenticated :
189- return jsonify ({'error' : 'Authentication required' , 'redirect' : '/login' }), 401
190-
191- return app
193+ return app # Don't forget to return the app!
192194
193195if __name__ == '__main__' :
194196 app = create_app ()
0 commit comments