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