Home > Mobile >  How to flash a message that user session was expired in flask
How to flash a message that user session was expired in flask

Time:10-16

I am working on building some basic functions. One of them what I am doing now is timeout-session. I set PERMANENT_SESSION_LIFETIME = timedelta(minutes=20) and I found it works well.
But what I am confused is How to tell users that the user's session was expired because of session lifetime using flash? Or is there a way to redirect when the user's session was expired?

Below is Specifics


tree

.
├── __init__.py
├── admin
│   ├── __init__.py
│   ├── forms.py
│   └── views.py
├── app.py
├── commands.py
├── compat.py
├── database.py
├── dataset
│   ├── __init__.py
│   ├── forms.py
│   ├── models.py
│   └── views.py
├── decorators.py
├── extensions.py
├── public
│   ├── __init__.py
│   ├── __pycache__
│   ├── forms.py
│   └── views.py
├── settings.py

settings.py


from datetime import timedelta

from environs import Env

env = Env()
env.read_env()

ENV = env.str("FLASK_ENV", default="production")
DEBUG = ENV == "development"
SQLALCHEMY_DATABASE_URI = env.str("DATABASE_URL")
SECRET_KEY = env.str("SECRET_KEY")
SEND_FILE_MAX_AGE_DEFAULT = env.int("SEND_FILE_MAX_AGE_DEFAULT")
BCRYPT_LOG_ROUNDS = env.int("BCRYPT_LOG_ROUNDS", default=13)
DEBUG_TB_ENABLED = DEBUG
DEBUG_TB_INTERCEPT_REDIRECTS = False
CACHE_TYPE = "simple"  # Can be "memcached", "redis", etc.
SQLALCHEMY_TRACK_MODIFICATIONS = False
MONGODB_URI = env.str("MONGODB_URI")
MONGODB_DATABASE_NAME = env.str("MONGODB_DATABASE_NAME")
UPLOAD_FOLDER = env.str("UPLOAD_FOLDER")
PERMANENT_SESSION_LIFETIME = timedelta(minutes=100)

app.py

# -*- coding: utf-8 -*-
"""The app module, containing the app factory function."""
import logging
import sys

from flask import Flask, render_template

from web import admin, commands, public, user, dataset
from web.extensions import (
    bcrypt,
    cache,
    csrf_protect,
    db,
    debug_toolbar,
    flask_static_digest,
    login_manager,
    migrate,
)


def create_app(config_object="web.settings"):
    """Create application factory
    :param config_object: The configuration object to use.
    """
    app = Flask(__name__.split(".")[0])
    app.config.from_object(config_object)
    register_extensions(app)
    register_blueprints(app)
    register_errorhandlers(app)
    register_shellcontext(app)
    register_commands(app)
    configure_logger(app)
    return app
``

CodePudding user response:

Use a decorator before_request, it runs before each request. https://flask.palletsprojects.com/en/2.0.x/api/#flask.Flask.before_request

@app.before_request
def load_user():
    if "user_id" in session:
        g.user = db.session.get(session["user_id"])

In your case, refer this answer.

@app.before_request
def before_request()

    now = datetime.datetime.now()
    try:
        last_active = session['last_active']
        delta = now - last_active
        if delta.seconds > 1800:
            session['last_active'] = now
            return logout('Your session has expired after 30 minutes, you have been logged out')
    except:
        pass

    try:
        session['last_active'] = now
    except:
        pass

https://stackoverflow.com/a/48768278/1474183

CodePudding user response:

I quoted this code from here.

login_mgr = LoginManager(app)
login_mgr.login_view = 'login'
login_mgr.refresh_view = 'relogin'
login_mgr.needs_refresh_message = (u"Session timedout, please re-login")
login_mgr.needs_refresh_message_category = "info"
  • Related