0

I'm trying to set up an API login of sorts that when the login is successful, it returns an JWT and first/last name sent with the login from the POST request. My problem is that I cant figure out a way that works to return the first/last name variables to another Class and function. This my code:

@app.route('/login', methods=['POST'])
def login():
    if not request.is_json:
        return jsonify({"msg": "Missing JSON in request"}), 400

    username = request.json.get('username', None)
    password = request.json.get('password', None)
    client_fname = request.json.get('Client First Name', None)
    client_lname = request.json.get('Client Last Name', None)
    if not username:
        return jsonify({"msg": "Missing username parameter"}), 400
    if not password:
        return jsonify({"msg": "Missing password parameter"}), 400

    if username != USER_DATA.get(username) and password not in USER_DATA[username]:
        return jsonify({"msg": "Bad username or password"}), 401

    access_token = create_access_token(identity=username)
    return jsonify(access_token=access_token), 200, PrivateResource.sendData(self, client_fname, client_lname)

class PrivateResource(Resource):
    @app.route('/protected', methods=['GET'])
    @jwt_required

    def sendData(self, client_fname, client_lname):
        return mysqldb.addUser("{}".format(client_fname),"{}".format(client_lname))

I want to return client_fname and client_lname so I can then use them with sendData(). How can I achieve this without have issues with unicode from json or passing the variables?

feners
  • 645
  • 5
  • 19
  • 48

2 Answers2

0

I think what you need is a Session then you can do something like this-

@app.route('/login', methods=['GET', 'POST'])
def login():
   if request.method == 'POST']
          session['username'] = request.form['user']
          return redirect(url_for('home'))

And in route for home use-

@app.route('/home')
def home():
    if 'username' in session:
        return render_template("index.html", name=session['username'])

Hope this helps

Bhaskar
  • 1,838
  • 1
  • 16
  • 29
0

You could use session to store the variables you need to access across functions. Add these to login,

session['client_fname'] = request.json.get('Client First Name', None)
session['client_lname'] = request.json.get('Client Last Name', None)

And access it from sendData as:

return mysqldb.addUser("{}".format(session['client_fname']),"{}".format(session['client_lname']))

Hope this answers your question.