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.
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')
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).
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.