简体   繁体   中英

Flask-login unable to set session cookie

I followed this tutorial on Real Python to set up Google OAuth login for my website. ( codebase for tutorial here )

I modified the code by breaking it into a blueprint and using application factory method to launch.

I used the tutorial's code by splitting it into a separate blueprint and using the application factory method. The user's details are collected via Google OAuth and stored in a db. The user data fetched from Google reflects perfectly in the local db, however, in the callback function, when the user object is passed to the login_user function, I get the following error –

Traceback (most recent call last):
  File "/home/user/otpd/venv/lib/python3.6/site-packages/flask/app.py", line 1969, in finalize_request
    response = self.process_response(response)
  File "/home/user/otpd/venv/lib/python3.6/site-packages/flask/app.py", line 2268, in process_response
    self.session_interface.save_session(self, ctx.session, response)
  File "/home/user/otpd/venv/lib/python3.6/site-packages/flask/sessions.py", line 387, in save_session
    samesite=samesite,
  File "/home/user/otpd/venv/lib/python3.6/site-packages/werkzeug/wrappers/base_response.py", line 481, in set_cookie
    samesite=samesite,
  File "/home/user/otpd/venv/lib/python3.6/site-packages/werkzeug/http.py", line 1163, in dump_cookie
    buf = [key + b"=" + _cookie_quote(value)]
TypeError: unsupported operand type(s) for +: 'NoneType' and 'bytes'

The code runs on a remote server using Gunicorn. When I run the code locally, there are no issues. The issue only crops up on the remote server.

Any suggestions on how to debug this?

Thanks!

Fixed. I had set an invalid SESSION_COOKIE_NAME in my config file. Changed that to the default value and everything works.

For the adventurers who end up on this issue in the future: Make sure you are setting up session environment variables correctly. You need an app secret and a session cookie name for Flask Sessions to run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM