我正在将 React 与 Flask 和 Flask-login 一起使用。我有一个登录路由,可以在前端成功登录用户。但是,一旦我导航到一个新页面,这会导致进行新的 API 调用,flask 就会返回 401。我在 Firefox Web 控制台的 Cookies 或 Session Storage 中看不到任何内容。
@flask_app.route('/<api_version>/login', methods=['POST'])
def login(api_version):
print(f'login', request.form )
username = request.form['username']
password = request.form['password']
response = None
try:
user = server.loginUser(username, password)
flask_login.login_user(user, remember=True)
except Exception as e:
errMsg = f'Error Logging in user {username}'
status = e.status if hasattr(e, 'status') else 500
print(f'{errMsg}')
traceback.print_exc()
return Response(
'{{"message": "{0}", "status": {1} }}'.format(
errMsg, status), status=status, mimetype='application/json')
resp_obj = json.dumps({"username": user.username, "role": user.role})
resp = Response(resp_obj, status=200, mimetype='application/json')
resp.headers['Content-Type'] = 'application/json'
return resp
@flask_app.route("/<api_version>/simulation-runs", methods=['GET'])
@flask_login.login_required
def get_simulation_runs(api_version):
...
服务器.py
from models import account # DB
def loginUser(username, password):
print(f'loginUser {username}')
user_db = account.get_account(username)
print(f'User returned from DB: {user_db}')
errMsg = 'Invalid username or password. Please try again.'
if not len(user_db) or (not 'password' in user_db):
e1 = Unauthorized(errMsg)
e1.status = 401
raise e1
莫回无
相关分类