|
10 | 10 | app = Flask(__name__) |
11 | 11 | app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db' |
12 | 12 | app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False |
| 13 | +app.config['PROPAGATE_EXCEPTIONS'] = True |
13 | 14 | api = Api(app) |
14 | | -db.init_app(app) |
15 | 15 |
|
16 | 16 | """ |
17 | | -JWT related configurations began. The following functions includes: |
| 17 | +JWT related configuration. The following functions includes: |
18 | 18 | 1) add claims to each jwt |
19 | 19 | 2) customize the token expired error message |
20 | 20 | """ |
21 | | -app.config['JWT_SECRET_KEY'] = 'jose' # we can also use app.secret like before, Flask-JWT-Extended can recognize both |
| 21 | +app.config['JWT_SECRET_KEY'] = 'jose' # we can also use app.secret like before, Flask-JWT-Extended can recognize both |
22 | 22 | app.config['JWT_BLACKLIST_ENABLED'] = True # enable blacklist feature |
23 | 23 | app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = ['access', 'refresh'] # allow blacklisting for access and refresh tokens |
24 | 24 | jwt = JWTManager(app) |
25 | 25 |
|
26 | 26 | """ |
27 | | -`claims` are data we choose to attached to each jwt payload |
| 27 | +`claims` are data we choose to attach to each jwt payload |
28 | 28 | and for each jwt protected endpoint, we can retrieve these claims via `get_jwt_claims()` |
29 | 29 | one possible use case for claims are access level control, which is shown below |
30 | 30 | """ |
31 | | - |
32 | | - |
33 | 31 | @jwt.user_claims_loader |
34 | 32 | def add_claims_to_jwt(identity): |
35 | | - if identity == 1: # instead of hard-coding, we can read from a config file to get a list of admins instead |
| 33 | + if identity == 1: # instead of hard-coding, we should read from a config file to get a list of admins instead |
36 | 34 | return {'is_admin': True} |
37 | 35 | return {'is_admin': False} |
38 | 36 |
|
@@ -104,4 +102,5 @@ def create_tables(): |
104 | 102 | api.add_resource(TokenRefresh, '/refresh') |
105 | 103 |
|
106 | 104 | if __name__ == '__main__': |
| 105 | + db.init_app(app) |
107 | 106 | app.run(port=5000, debug=True) |
0 commit comments