Click here to Skip to main content
15,503,491 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:

config_class.py

Python
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

Python
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__)
    app.config.from_object(config_options[config_class])


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

    from root_folder.clients import clients_app

    # register all the blueprints in this application
    app.register_blueprint(clients_app)

    CORS(app, supports_credentials=True)

    # return the app object to be executed
    return app


app.py
Python
from root_folder import create_app

app = create_app()

Procfile:
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):
Python
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
Posted

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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