|
in main.py, when wrote outside the request: app.permanent_session_lifetime = timedelta(days=5)
session.permanent = Trueit got an error RuntimeError: Working outside of request context |
Replies: 3 comments 2 replies
|
In BustAPI (similar to Flask), To configure and enable permanent sessions, set Option A: Set inside a View Function (e.g. login route)from datetime import timedelta
from bustapi import BustAPI, session
app = BustAPI()
app.secret_key = "some_secret_key"
app.permanent_session_lifetime = timedelta(days=5)
@app.route("/login", methods=["POST"])
def login():
# ... authenticate user ...
session["user_id"] = 123
session.permanent = True # Safe to set here inside the request context
return {"status": "success"}Option B: Set globally using a
|
|
session is request-scoped? session should be app-scoped, just hooked on request |
|
This is an excellent question and you actually highlighted a bug in the framework!
I have just fixed both of these issues in the codebase:
These changes have been merged into Thank you so much for bringing this up! Your observations are incredibly valuable for hardening the framework. |
Well, session data contains user-specific information (like who is logged in), which is read from the request's cookies and saved to the response's cookies. If the session object itself were app-scoped (shared across the entire application/all users), everyone would share the same session, which isn't what we want.
The
sessionobject in BustAPI is a context-local proxy (similar to Flask'ssession) that dynamically points to the session of the active request. That's why it raises aRuntimeErrorif you try to access/modify it at the module level when no HTTP request is being handled.If you want all sessions to be permanent, you can set it using a
@app.before_requesthook, which runs within…