-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_basic.py
More file actions
32 lines (24 loc) · 994 Bytes
/
app_basic.py
File metadata and controls
32 lines (24 loc) · 994 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from flask import Flask, request, jsonify
from flask_jwt_extended import JWTManager, create_access_token, get_jwt_identity, jwt_required
app = Flask(__name__)
app.config["JWT_SECRET_KEY"] = "sec"
jwt = JWTManager()
jwt.init_app(app=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"])
def login():
username = request.json.get("username", None)
password = request.json.get("password", None)
if username != "test" or password != "test":
return jsonify({"msg": "Bad username or password"}), 401
access_token = create_access_token(identity=username)
return jsonify(access_token=access_token)
@app.route("/protected", methods=["GET"])
@jwt_required()
def protected():
current_user = get_jwt_identity()
print(current_user)
return jsonify(logged_in_as=current_user), 200
if __name__ == "__main__":
app.run(debug=True)