Skip to content

Commit b57c2cf

Browse files
committed
Fix for #74 do not fail with empty request to /auth in simple.py , also using request.get_json()
1 parent 8261502 commit b57c2cf

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

examples/simple.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@
1313
# function is used to actually generate the token
1414
@app.route('/login', methods=['POST'])
1515
def login():
16-
username = request.json.get('username', None)
17-
password = request.json.get('password', None)
18-
if username != 'test' or password != 'test':
19-
return jsonify({"msg": "Bad username or password"}), 401
20-
21-
# Identity can be any data that is json serializable
22-
ret = {'access_token': create_access_token(identity=username)}
23-
return jsonify(ret), 200
24-
16+
if request.is_json:
17+
params = request.get_json()
18+
if 'username' in params.keys() and 'password' in params.keys():
19+
if params['username'] != 'test' or params['password'] != 'test':
20+
return jsonify({"msg": "Bad username or password"}), 401
21+
else:
22+
return jsonify({"msg": "Missing auth parameters"}), 401
23+
24+
# Identity can be any data that is json serializable
25+
ret = {'access_token': create_access_token(identity=params['username'])}
26+
return jsonify(ret), 200
27+
else:
28+
return jsonify({"msg": "Missing auth"}), 401
2529

2630
# Protect a view with jwt_required, which requires a valid access token
2731
# in the request to access.

0 commit comments

Comments
 (0)