Keep login sessions persistent across browser restarts
This commit is contained in:
+10
-2
@@ -5,7 +5,7 @@ from pathlib import Path
|
||||
from zoneinfo import ZoneInfo
|
||||
|
||||
import click
|
||||
from flask import Flask, render_template, request
|
||||
from flask import Flask, render_template, request, session
|
||||
|
||||
from .extensions import csrf, db, limiter, login_manager, migrate
|
||||
|
||||
@@ -21,7 +21,9 @@ def create_app(config=None):
|
||||
SESSION_COOKIE_HTTPONLY=True,
|
||||
SESSION_COOKIE_SAMESITE="Lax",
|
||||
SESSION_COOKIE_SECURE=production,
|
||||
PERMANENT_SESSION_LIFETIME=timedelta(days=7),
|
||||
# Effectively unlimited application lifetime; browsers may cap cookie retention.
|
||||
PERMANENT_SESSION_LIFETIME=timedelta(days=36500),
|
||||
SESSION_REFRESH_EACH_REQUEST=True,
|
||||
MAX_CONTENT_LENGTH=16 * 1024 * 1024,
|
||||
LOCAL_SETUP=not production,
|
||||
DATA_ROOT=os.environ.get("DATA_ROOT", str(ROOT / "data" / "garmin")),
|
||||
@@ -98,6 +100,12 @@ def create_app(config=None):
|
||||
def load_user(user_id):
|
||||
return db.session.get(User, int(user_id)) if user_id.isdigit() else None
|
||||
|
||||
@app.before_request
|
||||
def persist_login_session():
|
||||
# Also upgrade existing signed-in browser sessions on their next request.
|
||||
if "_user_id" in session:
|
||||
session.permanent = True
|
||||
|
||||
@app.template_filter("duration")
|
||||
def duration(value):
|
||||
value = round(value or 0)
|
||||
|
||||
Reference in New Issue
Block a user