B
B
Bruceee2019-04-17 00:18:29
Python
Bruceee, 2019-04-17 00:18:29

How to save and check for cookies?

There is a Flask application in which I display data from a third-party api. Through request.Session() I authorize on the server, in response I receive a set of cookies.
How can I save the received cookies in the browser during authorization so that the user does not have to log in again when the site is reopened?
And how to properly organize the check for cookies (=authorization) when visiting the site?
If it's just a script, then the sequence is clear, first I get the session:

session = requests.Session()
session.post('https://site.ru/auth/', {
    'email':   '[email protected]',
    'password': 'pass'
})

Then I make a get request from this session:
method_info = session.get('https://site.ru/method/')

It is not clear how to make an analogue of session.get() using previously saved cookies and how to check for their presence.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
P
pcdesign, 2019-04-17
@Bruceee

Usually, this is done by modules that are responsible for authorizing users.
But, if you want to implement it on bare flask. That is also no problem. This is how cookies are stored. When you need to read cookies from the user's browser, then like this:

user_id = session.get('user_id')
if not user_id:
    # Перекидываем на страницу с авторизацией
    redirect(url_for('login'))

To see what is stored in your local browser cookies, you can use this plugin
https://chrome.google.com/webstore/detail/editthis...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question