'Session management redirect

I implement a session management with that request:


app = Flask(__name__, template_folder='templates')

SECRET_KEY = 'kajsnxanxkanxkna'
app.config['SECRET_KEY'] = SECRET_KEY

@app.route('/', methods=['GET', 'POST'])
def index():
    return redirect(url_for('login'))

@app.before_request
def on_before_request():
    if request.endpoint != 'login':
        if 'mail' not in session:
            return redirect(url_for('login'))
        now = datetime.now(timezone.utc)
        if 'last_active' in session:
            last_active = session['last_active']
            delta = now - last_active
            if delta.seconds > 1000:
                del session['last_active']
                print("sesion invalidada")
                return redirect(url_for('login'))
        session['last_active'] = now

I get the 302 error when I initially go to the page, perhaps due to the redirection to the login when the session eventually times out. When I remove the before request the problem is solved but I need to implemented a session managment. How can I solve that



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source