You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fromflaskimportFlaskfromflaskimportjsonifyfromflaskimportrequestfromflask_jwt_extendedimportcreate_access_tokenfromflask_jwt_extendedimportget_jwt_identityfromflask_jwt_extendedimportjwt_requiredfromflask_jwt_extendedimportJWTManagerapp=Flask(__name__)
# Setup the Flask-JWT-Extended extensionapp.config["JWT_SECRET_KEY"] ="super-secret"# Change this!jwt=JWTManager(app)
# Create a route to authenticate your users and return JWTs. The# create_access_token() function is used to actually generate the JWT.@app.route("/login", methods=["POST"])deflogin():
username=request.json.get("username", None)
password=request.json.get("password", None)
ifusername!="test"orpassword!="test":
returnjsonify({"msg": "Bad username or password"}), 401access_token=create_access_token(identity=username)
returnjsonify(access_token=access_token)
# Protect a route with jwt_required, which will kick out requests# without a valid JWT present.@app.route("/protected", methods=["GET"])@jwt_required()defprotected():
# Access the identity of the current user with get_jwt_identitycurrent_user=get_jwt_identity()
returnjsonify(logged_in_as=current_user), 200if__name__=="__main__":
app.run()
### Run your application```pythonpythonapp.py