Click here to Skip to main content
15,795,789 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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:

class Config():
    CORS_ALLOW_HEADERS = ['Content-Type']
    CORS_ORIGINS = ['',
    SECRET_KEY = os.environ.get("APP_SECRET_KEY")
    SESSION_TYPE = 'redis'

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

    # initiate the flask app and assign the configurations #
    app = Flask(__name__)

    db.init_app(app)  # for database functions
    migrate.init_app(app, db)  # for migration functions

    from root_folder.clients import clients_app

    # register all the blueprints in this application

    CORS(app, supports_credentials=True)

    # return the app object to be executed
    return app
from root_folder import create_app

app = create_app()

<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);
withCredentials: true,
"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

# route for creating account credentials
@bp_auth_clients_app.route("/create_client", methods=["POST"])
def create_client():
    username = request.form.get("username").lower()
    email = request.form.get("email").lower()

    # create account code goes here  #
    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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900