I am building a web app with Flask. It is the dashboard of my marketing software. In my marketing software, user can create some campaigns.
Once the user is logged in, I get his campaigns ids and store them in a cookie.
PROBLEM:
The list of campaigns are not showing if user block the cookies.
So I am trying to find an alternative solution
WHAT I TRY:
I tried to store the values of the user in some global variables. I was hoping the other routes could have access to the values of these global variables.
This is a sample of my routes.py:
def create_global_variable():
global global_variable # must declare it to be a global first
# modifications are thus reflected on the module's global scope
global license_id
global email_license_ok
global product_id
global campaigns
global id_campaign
license_id =""
email_license_ok =""
product_id =""
campaigns = ""
id_campaign = ""
create_global_variable()
@app.route('/login', methods=['GET', 'POST'])
def login():
email_license_ok, license_id, product_id = check_email_license(user.user_email, form.licensekey.data)
print(f"""
********************** LOGIN SUCCESS ********************************
user.user_email : {user.user_email}
form.licensekey.data : {form.licensekey.data}
license_id : {license_id}
email_license_ok :{email_license_ok}
product_id : {product_id}
""")
if email_license_ok:
login_user(user, remember=form.remember.data)
next_page = request.args.get('next')
flash('Login successful!', 'success')
cookies = request.cookies
res = make_response(redirect(next_page) if next_page else redirect(url_for('dashboard')))
res.set_cookie("license_id", str(license_id))
res.set_cookie("product_id", str(product_id))
print(f"form.licensekey.data : {form.licensekey.data}")
return res
# The route to show campaigns's user
@app.route('/campaigns')
@login_required
def campaigns():
"""
This is the list of campaigns page
"""
# [ COOKIES ] -------------------------------------------------------------
page_campaigns_cookie = 1
cookies = request.cookies
#license_id = cookies.get("license_id")
if cookies.get("page_campaigns") and cookies.get("page_campaigns") != None:
page_campaigns_cookie = cookies.get("page_campaigns")
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# [ ARGUMENTS IN URL ] ------------------------------------------------------
args = request.args
#email_license_ok, license_id, product_id = check_email_license(user.user_email, form.licensekey.data)
print(f"""
********************** route /campaigns *******************************************
args : {args}
license_id : {license_id}
email_license_ok :{email_license_ok}
product_id : {product_id}
""")
When I simulate login and click on "Campaigns" I get this log:
********************** route /campaigns *******************************************
args : ImmutableMultiDict([])
license_id :
email_license_ok :
product_id :
As you can see, my global variables are empty. It didn't work.
How to make a variable available for all the functions of my routes.py file in Flask Python?