Session is shared between two Flask apps on localh

2019-07-13 07:47发布

问题:

So, I have two Flask apps running on localhost, one on the port 5001, and the other one on the port 5003, and apparently both are using the same session. If I log in on one app, it logs out on the other. And for example, recently, if I logged in using my email on one app, it would also log in my account on the other app, since I have users using that email on both apps, and I was using the email as an user identifier, but that stop happening when I used another id for the users.

I'm using Flask-Login, and Google Chrome (the same thing happens in Edge).

I'm not really sure if this could also happen in production, we'll probably use the same host for both apps, so that would be a problem. If this is something related only to localhost, then it's ok, but I don't think that's the case.

Any idea of what could be happening here?

Thanks in advance.

回答1:

I had a similar problem, and I think the cause is that both instances would use the same "session cookie" in the browser.

The solution that fixed it for me was renaming the SESSION_COOKIE_NAME which is session by default.

app.config.update(SESSION_COOKIE_NAME=<new_session_name>)

I found the solution via: https://stackoverflow.com/a/45497948/380038