Heroku gunicorn flask login is not working properly
<p>I have a flask app that uses Flask-Login for authentication. Everything works fine locally both using flask's built in web server and gunicorn run locally. But when it's on heroku it's faulty, sometimes it logs me in and sometimes it does not. When I successfully logged in within a few seconds of navigating my session just gets destroyed and have me logged out automatically. This should just happen when the user logged out.</p>

<p>The following code snippet in my view might be relevant:</p>

<pre><code>@app.before_request
def before_request():
    g.user = current_user

# I have index (/) and other views (/new) decorated with @login_required
</code></pre>

<p>I might be having similar issues <a href="http://stackoverflow.com/questions/13614877/flask-login-and-heroku-issues">with this</a>. It does not have any answers yet and from what I read from the comments, the author just ran his app with <code>python app.py</code>. That is through using flask's built-in web server. However I can't seem to duplicate his workaround since running <code>app.run(host='0.0.0.0')</code> runs the app in port <code>5000</code> and I can't seem to set <code>port=80</code> because of permission.</p>

<p>I don't see anything helpful with the logs except that it does not authenticate even when I should.</p>

<p>Part of the logs when I got authenticated and tried to navigate to <code>/new</code> and <code>/</code> alternately until it logs me out:</p>

<pre><code>2016-09-25T06:57:53.052378+00:00 app[web.1]: authenticated - IP:10.179.239.229
2016-09-25T06:57:53.455145+00:00 heroku[router]: at=info method=GET path="/" host=testdep0.herokuapp.com request_id=c7c8f4c9-b003-446e-92d8-af0a81985e72 fwd="124.100.201.61" dyno=web.1 connect=0ms service=116ms status=200 bytes=6526
2016-09-25T06:58:11.415837+00:00 heroku[router]: at=info method=GET path="/new" host=testdep0.herokuapp.com request_id=ae5e4e29-0345-4a09-90c4-36fb64785079 fwd="124.100.201.61" dyno=web.1 connect=0ms service=7ms status=200 bytes=2552
2016-09-25T06:58:13.543098+00:00 heroku[router]: at=info method=GET path="/" host=testdep0.herokuapp.com request_id=47696ab9-57b9-4f20-810a-66033e3e9e50 fwd="124.100.201.61" dyno=web.1 connect=0ms service=8ms status=200 bytes=5982
2016-09-25T06:58:18.037766+00:00 heroku[router]: at=info method=GET path="/new" host=testdep0.herokuapp.com request_id=98912601-6342-4d71-a106-26056e4bbb21 fwd="124.100.201.61" dyno=web.1 connect=0ms service=3ms status=200 bytes=2552
2016-09-25T06:58:19.619369+00:00 heroku[router]: at=info method=GET path="/" host=testdep0.herokuapp.com request_id=2b04d31f-93a2-4653-83a4-f95ca9b97149 fwd="124.100.201.61" dyno=web.1 connect=0ms service=3ms status=302 bytes=640
2016-09-25T06:58:19.953910+00:00 heroku[router]: at=info method=GET path="/login?next=%2F" host=testdep0.herokuapp.com request_id=e80d15cd-e9ad-45ff-ae54-e156412fe4ff fwd="124.100.201.61" dyno=web.1 connect=0ms service=3ms status=200 bytes=2793
</code></pre>

<p>Procfile:</p>

<p><code>web: gunicorn app:app</code></p>

<p>The problem was solved by adding the <code>--preload</code> option to gunicorn. I'm not entirely sure how that solved the problem and would appreciate if someone can explain.</p>

<p>Updated Procfile:</p>

<p><code>web: gunicorn app:app --preload</code></p>

python heroku flask gunicorn flask-login