0

I have an auth.py file where I store

session["current_user"] = user_data

If the user logs in I can see the session data in the console. How can I pass this session data to another file test.py?

In test.py some processing happens and not routed, since the user doesn't needs access on this.

zamir
  • 2,144
  • 1
  • 11
  • 23
Umut885
  • 165
  • 1
  • 15
  • This is too vague. How do you want to call this other code? – Daniel Roseman Dec 03 '19 at 17:14
  • The docs say it works somehow by just importing the session module from flask e.g. "from flask import session". If I could import it from auth.py would be also fine. – Umut885 Dec 03 '19 at 17:15
  • That works *when you are inside a request*. How would you expect it to work if you weren't in a request. What session data would even be used? – Daniel Roseman Dec 03 '19 at 17:22
  • Actually the session data will be stored just once and will be only read once. If I could assign it to a variable and import it in test.py would have helped – Umut885 Dec 03 '19 at 17:24
  • Then it's not *session* data. Just store it in the database somewhere. – Daniel Roseman Dec 03 '19 at 17:26

1 Answers1

2

As long as you call a function within that file while a request is being processed (i.e. from one of your endpoints), it will have access to the session.

If you do not call it while a request is being processed, it will not have access to the session and you cannot do anything about it.

The reason is that on every request, the browser sends you a cookie. In this cookie there is a unique identifier that allows Flask to populate the value of session differently for each browser.

If you're not handling a request, you do not have a cookie, so you can't get a value in session.

Jose Salvatierra
  • 2,407
  • 6
  • 21
  • 41