-
Notifications
You must be signed in to change notification settings - Fork 0
Session
Alberto Ruiz edited this page Nov 12, 2025
·
1 revision
TeaLeaf provides a lightweight built-in session system that allows you to persist data between requests per user, without depending on external libraries or databases.
When a new client connects, TeaLeaf:
- Generates a unique session ID (UID).
- Stores a Python dictionary associated with that UID on the server.
- Sends a cookie to the client containing the session UID.
- For every subsequent request, TeaLeaf retrieves the stored session object using that cookie.
The result is a simple but powerful mechanism to maintain per-user state.
TeaLeaf sessions are represented by the Session class, which behaves like a Python dictionary but also supports attribute-style access.
store.has(attr) # check if attr exists in the store return True or False
store[attr] = value # set the attr to value
store[attr] # returns the value , throw exception if attr not exist in the storeif store exists as an argument in the function for a route, TeaLeaf will inject the store automatically
@app.route("/login")
def login(session, req: HttpRequest):
user = req.form()
if not user or "userName" not in user:
return "401 unauthorized"
# Store user data in the session
session.userName = user["userName"]
return redirect("/")- Sessions are identified only by a cookie; if the cookie is deleted, the session resets.
- Currently, sessions are not persisted (they live only in server memory).
- Persisting sessions
- Session expiration time
- Signed/encrypted cookies