This repository was archived by the owner on Aug 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
48 lines (37 loc) · 1.38 KB
/
test.py
File metadata and controls
48 lines (37 loc) · 1.38 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from flask import Flask, jsonify, request
from flask_jwt_extended import (
JWTManager, jwt_required, create_access_token,
get_jwt_identity
)
import sys
app = Flask(__name__)
# Setup the Flask-JWT-Extended extension
app.config['JWT_SECRET_KEY'] = 'NTUT_NPC' # Change this!
jwt = JWTManager(app)
@jwt.invalid_token_loader
def invalid_token_callback(expired_token):
return "", 401
# Provide a method to create access tokens. The create_access_token()
# function is used to actually generate the token, and you can return
# it to the caller however you choose.
@app.route('/login', methods=['POST'])
def login():
# if not request.is_json:
# return jsonify({"msg": "Missing JSON in request"}), 400
user_info = dict(request.form)
print(user_info, sys.stdout)
# post 到 威任的後端,並把資料存進 json file
# Identity can be any data that is json serializable
access_token = create_access_token(identity=user_info)
return access_token
# Protect a view with jwt_required, which requires a valid access token
# in the request to access.
@app.route('/protected', methods=['GET'])
@jwt_required
def protected():
# Access the identity of the current user with get_jwt_identity
current_user = get_jwt_identity()
print(type(current_user), sys.stdout)
return jsonify(logged_in_as=current_user), 200
if __name__ == '__main__':
app.run()