✅已解决 | Authing的后端sdk怎么封装账户的上下文

如何使用sdk切换账户身份上下文。有大佬帮忙答疑一下嘛

大致就是下面的代码

from authing.v2.authentication import AuthenticationClient, AuthenticationClientOptions
from flask import Flask, request, jsonify

app = Flask(__name__)

authentication_client = AuthenticationClient(
  options=AuthenticationClientOptions(
    app_id='xxxxxxx',
    app_host='https://xxxxxx.authing.cn'
))


@app.route('/login')
def login():
  data = request.get_json()
  email = data['email']
  password = data['password']
  try:
    login_msg = authentication_client.login_by_email(email, password)
    return jsonify({
      "code": "200",
      "data": login_msg 
    })
  except Exception as e:
    return jsonify({
      "code": "500",
      "error": e
    }), 500

@app.route('check_status')
def check_status():
  data = request.get_json()
  if data.get('token') == None:
    return jsonify({
      "error": "please enter [token,]"
    })
  status = authentication_client.check_login_status(token=data['token'])
  if status['code'] == 200:
    return jsonify(status), 200
  else:
    return jsonify({
      "code": "400",
      "result": "not login" 
    }), 400

@app.route('/logout')
def logout():
  try:
    res = authentication_client.logout()
    if res:
      return jsonify({
        "code": "200",
        "result": "logout successfully"
      })
    else:
      return jsonify({
        "code": "400",
        "result": "logout failed"
        })
  except:
    return jsonify({
      "code": "400",
      "result": "logout error" 
    })



authentication_client 应该是需要在每次请求的时候初始化的,把初始化放到 api 的 handler 里面:

@app.route('/login')
def login():
  data = request.get_json()
  email = data['email']
  password = data['password']
  authentication_client = AuthenticationClient(
    options=AuthenticationClientOptions(
      app_id='xxxxxxx',
      app_host='https://xxxxxx.authing.cn'
  ))
  try:
    login_msg = authentication_client.login_by_email(email, password)
    return jsonify({
      "code": "200",
      "data": login_msg 
    })
  except Exception as e:
    return jsonify({
      "code": "500",
      "error": e
    }), 500