I just hosted my flask app for the first time online, everything seems to be working fine so far except for the sessions, which are not persistent.
I have tested the app severally on my local server using the inbuilt flask and gunicorn server . It works perfectly fine on both, but when I upload the code , the session stops being persistent. On the front end I use vue, using axios for making ajax calls, while flask runs my back end. I use redis for my session store.
Below are snippets of my code:
config_class.py
class Config():
CORS_ALLOW_HEADERS = ['Content-Type']
CORS_ORIGINS = ['https://sabenzi-org.web.app',
'http://127.0.0.1:8080']
SECRET_KEY = os.environ.get("APP_SECRET_KEY")
SESSION_TYPE = 'redis'
_init.py
from flask import Flask, session
from flask_session import Session
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_cors import CORS
from root_folder.config import config_options
db = SQLAlchemy()
migrate = Migrate()
ma = Marshmallow()
sess = Session()
def create_app(config_class='production'):
app = Flask(__name__)
app.config.from_object(config_options[config_class])
db.init_app(app)
migrate.init_app(app, db)
sess.init_app(app)
from root_folder.clients import clients_app
app.register_blueprint(clients_app)
CORS(app, supports_credentials=True)
return app
app.py
from root_folder import create_app
app = create_app()
Procfile:
<web: gunicorn -w 1 app:app
axios front end code
<pre lang="Javascript">
let formData = new FormData();
formData.append("email", email);
formData.append("password", password);
axios.post(
backendUrl+'create_client_account',
formData,
{
withCredentials: true,
headers:{
"Content-Type": "multipart/form-data"
}
}
);
create client route ( I have stripped this code block to the bare minimum to make it understandable):
from flask import session
@bp_auth_clients_app.route("/create_client", methods=["POST"])
def create_client():
username = request.form.get("username").lower()
email = request.form.get("email").lower()
auth_authentication = True
session["auth_authentication"] = auth_authentication
req_feedback = {
"status": True,
"message": "Account was successfully created",
"data": feedback_data
}
return jsonify(req_feedback), 200
After the account is successfully created, I am unable to access the session value in subsequent requests, it returns None.
From my research, I see that so many people have similar issues, but none of the solutions I've seen on stackoverflow worked for me.
Kindly note that I only have this issue when I host on heroku, my code works fine on my local server as previously stated.
I'll appreciate any support with this request
What I have tried:
My code base is included in the question above