Separate Flask items, written in the browser's cookie, how to find the corresponding session on the server during the request process

Project structure: react frontend + flask backend
problem description: when I log in, I call session ["token"] = username, and I see the cookie written in the browser"s cookie (the key-value pair is session:asasjndjan). When I click to log out again, request.cookies.get (" session") gets the corresponding cookies,. My problem is, according to this cookies, how to get the previous session, so that I can get the username; in session again

.

login code:

def login():
        raw_data = request.get_data()
        req_objt = json.loads(raw_data)
        username = req_objt.get("username")
        password = req_objt.get("password")

        user = User.query.filter(User.username == username, User.password == password).first()
        if user:
                session["token"] = username
Mar.01,2021

items separated from front and rear ends have been abandoned session.


in items separated from front and rear ends, authentication is generally performed by token
if you have to use session, you need to bring cookie to the client request


the above operations do not need to be done manually. Flask will help you automatically. The general process is as follows

-sharp set session
-sharp responsesessioncookie
-sharp 
flask.session['test'] = 'test1'
-sharp 
flask.session['test'] = 'test2'

-sharp cookiecookiesession
-sharp sessioncookie
-sharp 
assert 'test1' == flask.session.get('test')
-sharp 
assert 'test2' == flask.session.get('test')

for the implementation of the above process, please refer to from flask.sessions import SecureCookieSessionInterface
below are some of the source codes I posted and comments for personal understanding

-sharp session
def open_session(self, app, request):
  -sharp sessionsession
  s = self.get_signing_serializer(app)
  if s is None:
    return None
  -sharp requestsession cookie
  val = request.cookies.get(app.session_cookie_name)
  if not val:
    return self.session_class()
  -sharp session 
  max_age = total_seconds(app.permanent_session_lifetime)
  try:
    -sharp request cookiesession
    data = s.loads(val, max_age=max_age)
    return self.session_class(data)
  except BadSignature:
    return self.session_class()

of course, it is possible to get cookie, first in the way you get content through cookie, but it should not be necessary for session.
implementation proposal

-sharp redis
-sharp 
-sharp cookie
cookie = get_cookie() or init_cookie()
-sharp session_myselfdictredis
if not session_myself:
  session_myself = dict()
-sharp 
session_current = session_myself.get(cookie, {})
session_current['test'] = 'test'
-sharp cookie

-sharp 
Menu