urag
urag

Reputation: 1258

Flask session is not persisting

Ok, guys, I have been bashing my head into the wall for 3 hours already and still can't find a solution. So here is my scenario I developed an app for Shopify it uses an iframe to show my app. I am using a Flask session to persist a current user name now for some reason on my computer (I don't mean localhost I mean browser on my pc) it's not working in chrome it does work on other computers and it does work on other browsers just for demonstration here is a piss of code

  app.secret_key = "some secret key"
  @blueprint.route("/shopify")
  def shopify_entry():
    session["SHOP_NAME"] = shop_name
    logging.info(f"Session keys {session.keys()}") #!!!! All good it has a value
    return render_template("gen/index.html")

For client side I use Vue.js when index.html loads it makes a REST request for shop data here is a part that should return this data

@blueprint.route("/get_shop_settings")
def get_shop_settings():
    logging.info(f"Session keys {session.keys()}") #!!!! Why is this empty
    shop_name = session[SHOP_NAME]#This will throw an exception !!!!
    shop_info = extract_shop_settings_for_ui(get_shop(shop_name))
    logging.info(f"Returning shop data {shop_info}")
    return shop_info

Note it does work in firefox and it does work on other computer on same Crome version but on my PC it's not working even if I switch to Incognito it was working for half a year but now it suddenly stopped. And the strangest thing is that session cookie is created

Upvotes: 2

Views: 2199

Answers (1)

urag
urag

Reputation: 1258

Found a solution when using an iframe chrome will only store it's cookies if the session SameSite is set to 'None' string and it is secured so from whoever struggles with the same problem you need to do two things first set the app config as follows

app = Flask(__name__, template_folder="../templates", static_folder="../static")
app.config.update(SESSION_COOKIE_SAMESITE="None", SESSION_COOKIE_SECURE=True)

and second you need to update your Werkzeug library to 1.0.0 or higher

Upvotes: 4

Related Questions