Session variable isn't accessible by other Flask routes

I can't figure out why my session variables aren't transferring to other flask routes. I believe I have it set up properly, but I'm having a problem with this code: from flask import Flask, request, jsonify, redirect, url_for, render_template, session from flask_session import Session import requests import os import json app = Flask(__name__) app.secret_key = os.urandom(24) # Session setup SESSION_TYPE = 'filesystem' app.config.from_object(__name__) Session(app) @app.route('/') def index(): shop_origin = session.get('shop_origin', 'Shop Origin In Session Not Set') print("index shop_origin is equal to: %s" % shop_origin, flush=True) if not shop_origin: session['have_shop_info_in_session'] = False if 'have_shop_info_in_session' in session: print("have_shop_info_in_session is equal to: %s" % session['have_shop_info_in_session'], flush=True) if 'have_shop_info_in_session' in session and session['have_shop_info_in_session']: shop_info = session.get('shop_info', 'No Shop Info Found') return render_template('app_interface.html') else: return render_template('fetch_shop_info.html') @app.route('/set_shop_info', methods=['POST']) def set_shop_info(): data = request.get_json() shop_origin = data.get('shopOrigin') if not shop_origin: return jsonify({'error': 'Shop origin not provided'}), 400 session['shop_origin'] = shop_origin print("Loaded session shop_origin: %s" % session.get('shop_origin'), flush=True) session['have_shop_info_in_session'] = True # Set the flag to true print("set_shop_info have_shop_info_in_session is equal to: %s" % session['have_shop_info_in_session'], flush=True) return jsonify({'status': 'success'}), 200 When this code runs, I get positive flags in the logs with Loaded session shop_origin: testing-store.myshopify.com and set_shop_info have_shop_info_in_session is equal to: True, but as soon as the Javascript from the fetch_shop_info.html redirects to the index flask route, the logs show index shop_origin is equal to: Shop Origin In Session Not Set. Why is the variable not transferring to the other route in the session? Cookies are enabled in the browser. EDIT: I tried running the code in a different browser and it seems to work fine, so the problem has to be my browser (Brave Browser). I have Brave Shields turned off for Shopify and I can see that the cookie SEEMS to be stored correctly by the browser. I'm still not sure what the problem is exactly but I'm a little bit closer to figuring out what it is.

Comment (1)

Jese Leos

August 19, 2024

Verified user

The code works properly in Firefox, but not Brave browser, so the issue is with the browser. I have no idea what this code does, but this seems to make the code work by adding it towards the top of my code. Here's a link to where I found the solution. app.config.update(SESSION_COOKIE_SAMESITE="None", SESSION_COOKIE_SECURE=True)

You’ll be in good company