-1

I have a flask token authenticated application implemented by flask jwt extended.

Code

import random
import os
from flask import Flask, jsonify, request, session
from flask_cors import CORS
from flask_jwt_extended import create_access_token
from flask_jwt_extended import JWTManager
import json

app = Flask(__name__)
app.config["JWT_SECRET_KEY"] = "some secret key"
jwt = JWTManager(app)
CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
secret_email = "xxxx@gmail.com"
secret_pass = "password"

@app.route("/")
def home():
    return jsonify({"msg":"Hello, World!"})

@app.route("/login", methods=["POST"])
def login():
    email = request.json.get("email", None)
    password = request.json.get("password", None)
    if email != secret_email or password != secret_pass:
        return jsonify({"msg": "Bad username or password"}), 401

    access_token = create_access_token(identity=email)
    return jsonify(access_token=access_token)

@app.route("/configs")
def configs():
    a = random.randint(10,30)
    b = random.randint(1,5)
    summ = a+b
    return jsonify({"a":a,"b":b})

@app.route("/sumcollect")
def sumcollect():
    return jsonify({"sum is":summ})
    
if __name__ == "__main__":
    app.run(debug=False)

The summ variable in route /configs needs to be accessed in route /sumcollect . I tried with session variables. But the token authentication and session variables are not working together. Is there any way to pass the variables in this case.

Note: Just added django tag for this in case for them to look at to have a solution.

davidism
  • 121,510
  • 29
  • 395
  • 339
Aniiya0978
  • 274
  • 1
  • 9

1 Answers1

0

In Flask , the method to send variables from one route to another is possible using the redirect function. Redirect function needs the url of the function and the values that is needed by the other route. For more information , you can refer : https://sodocumentation.net/flask/topic/6856/redirect

@app.route("/sumcollect")
def sumcollect():
    summ = request.args['summ']
    return jsonify({"sum is":summ})

@app.route("/configs")
def configs():
    a = random.randint(10,30)
    b = random.randint(1,5)
    summ = a+b
    return jsonify({"a":a,"b":b}),redirect(url_for('sumcollect',summ=summ))
Nilaya
  • 148
  • 8
  • both routes are called independently. First /configs route is called and then only /sumcollect is called. Its a fixed pattern. I am getting 500 Internal Server Error while trying to access. – Aniiya0978 Sep 23 '21 at 05:12