@@ -25,6 +25,14 @@ def add_claims_to_access_token(user):
2525 return {'roles' : user .roles }
2626
2727
28+ # This method will also get whatever object is passed into the
29+ # create_access_token method, and let us define what the identity
30+ # should be for this object
31+ @jwt .user_identity_loader
32+ def user_identity_lookup (user ):
33+ return user .username
34+
35+
2836@app .route ('/login' , methods = ['POST' ])
2937def login ():
3038 username = request .json .get ('username' , None )
@@ -38,17 +46,10 @@ def login():
3846 # We can now pass this complex object directly to the
3947 # create_access_token method. This will allow us to access
4048 # the properties of this object in the user_claims_loader
41- # function. Because this object is not json serializable itself,
42- # we also need to provide a way to get some which is json
43- # serializable and represents the identity of this token from
44- # the complex object. We pass a function to the optional
45- # identity_lookup kwarg, which tells the create_access_token
49+ # function, and get the identity of this object from the
50+ # user_identity_loader function.
4651 # function how to get the identity from this object
47- access_token = create_access_token (
48- identity = user ,
49- identity_lookup = lambda u : u .username
50- )
51-
52+ access_token = create_access_token (identity = user )
5253 ret = {'access_token' : access_token }
5354 return jsonify (ret ), 200
5455
0 commit comments