Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

406
Views
Flask session values are not saved on different requests

I have searched for solutions everywhere but couldn't solve my problem. I am currently running a Flask application with uWSGI and NGINX in AWS.

My flask app is running locally 127.0.0.1:5000. I have tried adding secret_key as mentioned in different questions but it didn't work.

Here are some of my code:

app.py

from flask import Flask, request, redirect, url_for, session, abort
from flask_session import Session

app = Flask(__name__)
app.config["SESSION_TYPE"] = 'filesystem'
app.config["SESSION_FILE_DIR"] = '/var/www/test/session'

Session(app)

@app.route('/auth/login', methods=['POST'], strict_slashes=False)
def login():
    # did some validations here
    session.permanent = True
    session['is_logged_in'] = True
    return redirect(url_for('home'))

@app.route('/auth/home', methods=['GET'], strict_slashes=False)
def home():
    if session.get('is_logged_in') == None:
        return redirect('/') # will return to default page
    return render_template('home.html')

if __name__ == '__main__':
    app.run(host='0.0.0.0')

I always get redirected to the default page since the session did not store 'is_logged_in' value. But it did store { permanent: True } which I believe is from the line session.permanent = True.

Configuration in nginx.conf:

location /auth {
    rewrite ^/(.+) $1 break;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off:
    proxy_pass http://127.0.0.1:5000/$1$is_args$args;
}

If it helps, my uWSGI is running in socket = 120.0.0.1:5000 and I am using protocol = http.

And when I checked application cookies in browser, it has a session cookie registered.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Try using if session['is_logged_in'] == False

And set the session is logged as false in the start. On the top after Session(app) add this session['is_logged_in'] = False

Edit:

You check if 'logged_in' is present in session. When logging in do this:

if 'logged_in' in session:
    #User logged in 

In logout do this

session.pop('logged_in')
over 4 years ago · Santiago Trujillo Report

0

After some tedious research, it turns out I just needed to include plugin = python36 in my uwsgi.ini which is from uwsgi-plugin-python36 (rpm).

over 4 years ago · Santiago Trujillo Report

0

I noticed a lack of session persistence in my OAuth 2.0 flow. The SAMESITE ="Strict" setting was what was causing it. In one of the steps the Identity Provider was failing to set cookie and respond with the same session id. This led to a new session being created at the end of the auth flow - hence no authentication was happening.

Setting SAMESITE to "Lax" solved my problem.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!