When a user logs into my sample application I add a username key into the session object and I hang a user object off of the global "g" object. See below.
@app.route('/login/', methods=['GET', 'POST'])
def login():
"""Logs the user in."""
error = None
if request.method == 'POST':
print("login hit")
user = get_user(request.form['username'])
if user is None:
error = 'Invalid username'
print error
elif not check_password_hash(user.password, request.form['password']):
error = 'Invalid password'
print error
else:
flash('You were logged in')
print "logged in"
session['username'] = request.form['username']
g.user = request.form['username']
print error, "error"
return redirect(url_for('main'))
Now, if the user logs in successfully (which is what is happening), he will redirect to the main page. This will teardown the old request and spawn a new one. The problem is that when a new request starts the session is wiped. I am printing out the session keys and they are all empty. This only happens on heroku, not on my local machine. What am I doing wrong.
@app.before_request
def before_request():
print session.keys(), session.values() # shows [] []
print("before request")
print ('username' in session, "in session?")
g.db = connect_db()
g.user = None
if "username" in session:
g.user = get_user(session['username'])