@@ -30,7 +30,7 @@ def check_if_token_revoked(jwt_header, jwt_payload):
3030 Flask-Restx models for api request and response data
3131"""
3232
33- signup_model = rest_api .model ('SignUpModel' , {"name " : fields .String (required = True , min_length = 2 , max_length = 32 ),
33+ signup_model = rest_api .model ('SignUpModel' , {"username " : fields .String (required = True , min_length = 2 , max_length = 32 ),
3434 "email" : fields .String (required = True , min_length = 4 , max_length = 64 ),
3535 "password" : fields .String (required = True , min_length = 4 , max_length = 16 )
3636 })
@@ -39,7 +39,7 @@ def check_if_token_revoked(jwt_header, jwt_payload):
3939 "password" : fields .String (required = True , min_length = 4 , max_length = 16 )
4040 })
4141
42- user_edit_model = rest_api .model ('UserEditModel' , {"name " : fields .String (required = True , min_length = 2 , max_length = 32 ),
42+ user_edit_model = rest_api .model ('UserEditModel' , {"username " : fields .String (required = True , min_length = 2 , max_length = 32 ),
4343 "password" : fields .String (required = True , min_length = 4 , max_length = 16 )
4444 })
4545
@@ -59,20 +59,22 @@ def post(self):
5959
6060 req_data = request .get_json ()
6161
62- _name = req_data .get ("name " )
62+ _username = req_data .get ("username " )
6363 _email = req_data .get ("email" )
6464 _password = req_data .get ("password" )
6565
6666 user_exists = Users .get_by_email (_email )
6767 if user_exists :
68- abort (400 , "Sorry. This email already exists." )
68+ return {"success" : False ,
69+ "msg" : "Sorry. This email already exists." }, 400
6970
70- new_user = Users (name = _name , email = _email )
71+ new_user = Users (username = _username , email = _email )
7172
7273 new_user .set_password (_password )
7374 new_user .save ()
7475
75- return {'message' : 'User with (%s, %s) created successfully!' % (_name , _email )}, 201
76+ return {"success" : True ,
77+ "msg" : "User with (%s, %s) created successfully!" % (_username , _email )}, 201
7678
7779
7880@rest_api .route ('/api/users/login' )
@@ -92,10 +94,12 @@ def post(self):
9294 user_exists = Users .get_by_email (_email )
9395
9496 if not user_exists :
95- abort (400 , "Sorry. This email does not exist." )
97+ return {"success" : False ,
98+ "msg" : "Sorry. This email does not exist." }, 400
9699
97100 if not user_exists .check_password (_password ):
98- abort (400 , "Sorry. Wrong credentials." )
101+ return {"success" : False ,
102+ "msg" : "Sorry. Wrong credentials." }, 400
99103
100104 # create access token uwing JWT
101105 access_token = create_access_token (identity = _email )
@@ -106,7 +110,7 @@ def post(self):
106110@rest_api .route ('/api/users/edit' )
107111class EditUser (Resource ):
108112 """
109- Edits User's name or password or both using 'user_edit_model' input
113+ Edits User's username or password or both using 'user_edit_model' input
110114 """
111115
112116 @rest_api .expect (user_edit_model )
@@ -117,28 +121,30 @@ def post(self):
117121 current_user = Users .get_by_email (user_email )
118122
119123 if not current_user :
120- abort (400 , "Sorry. Wrong auth token. This user does not exist." )
124+ return {"success" : False ,
125+ "msg" : "Sorry. Wrong auth token. This user does not exist." }, 400
121126
122127 req_data = request .get_json ()
123128
124- _new_name = req_data .get ("name " )
129+ _new_username = req_data .get ("username " )
125130 _new_password = req_data .get ("password" )
126131
127- if _new_name :
128- current_user .update_name ( _new_name )
132+ if _new_username :
133+ current_user .update_username ( _new_username )
129134
130135 if _new_password :
131136 current_user .set_password (_new_password )
132137
133138 current_user .save ()
134139
135- return {"message" : 'User details updated successfully!' }, 200
140+ return {"success" : True ,
141+ "msg" : "User details updated successfully!" }, 200
136142
137143
138144@rest_api .route ('/api/users/logout' )
139145class LogoutUser (Resource ):
140146 """
141- Edits User's name or password or both using 'user_edit_model' input
147+ Edits User's username or password or both using 'user_edit_model' input
142148 """
143149
144150 @rest_api .expect (user_edit_model )
@@ -149,9 +155,11 @@ def delete(self):
149155 current_user = Users .get_by_email (user_email )
150156
151157 if not current_user :
152- abort (400 , "Sorry. Wrong auth token" )
158+ return {"success" : False ,
159+ "msg" : "Sorry. Wrong auth token" }, 400
153160
154161 jwt_block = JWTTokenBlocklist (jti = get_jwt ()["jti" ], created_at = datetime .now (timezone .utc ))
155162 jwt_block .save ()
156163
157- return {"message" : "JWT Token revoked successfully!" }, 200
164+ return {"success" : True ,
165+ "msg" : "JWT Token revoked successfully!" }, 200
0 commit comments