@@ -118,6 +118,7 @@ func (u *Users) postUser() http.HandlerFunc {
118118 msg := fmt .Sprintf ("an error occurred while hashing password: %v" , userBody .Password )
119119 log .Errorln (logTag , ":" , msg , ":" , err )
120120 util .WriteBackError (w , msg , http .StatusInternalServerError )
121+ return
121122 }
122123
123124 var newUser * user.User
@@ -211,10 +212,24 @@ func (u *Users) patchUser() http.HandlerFunc {
211212 }
212213 }
213214
215+ // If user is trying to update the password then store the hashed password
216+ if patch ["password" ] != nil {
217+ hashedPassword , err := bcrypt .GenerateFromPassword ([]byte (userBody .Password ), bcrypt .DefaultCost )
218+ if err != nil {
219+ msg := fmt .Sprintf ("an error occurred while hashing password: %v" , userBody .Password )
220+ log .Errorln (logTag , ":" , msg , ":" , err )
221+ util .WriteBackError (w , msg , http .StatusInternalServerError )
222+ return
223+ }
224+ patch ["password" ] = string (hashedPassword )
225+ }
226+
214227 _ , err2 := u .es .patchUser (req .Context (), username , patch )
215228 if err2 == nil {
216229 // Clear username record from the cache
217230 auth .ClearPassword (username )
231+ // Clear user record from the user cache
232+ auth .RemoveCredentialFromCache (username )
218233 util .WriteBackMessage (w , "User is updated successfully" , http .StatusOK )
219234 return
220235 }
@@ -284,10 +299,24 @@ func (u *Users) patchUserWithUsername() http.HandlerFunc {
284299 }
285300 }
286301
302+ // If user is trying to update the password then store the hashed password
303+ if patch ["password" ] != nil {
304+ hashedPassword , err := bcrypt .GenerateFromPassword ([]byte (userBody .Password ), bcrypt .DefaultCost )
305+ if err != nil {
306+ msg := fmt .Sprintf ("an error occurred while hashing password: %v" , userBody .Password )
307+ log .Errorln (logTag , ":" , msg , ":" , err )
308+ util .WriteBackError (w , msg , http .StatusInternalServerError )
309+ return
310+ }
311+ patch ["password" ] = string (hashedPassword )
312+ }
313+
287314 _ , err2 := u .es .patchUser (req .Context (), username , patch )
288315 if err2 == nil {
289316 // Clear username record from the cache
290317 auth .ClearPassword (username )
318+ // Clear user record from the user cache
319+ auth .RemoveCredentialFromCache (username )
291320 util .WriteBackMessage (w , "User is updated successfully" , http .StatusOK )
292321 return
293322 }
@@ -306,6 +335,8 @@ func (u *Users) deleteUser() http.HandlerFunc {
306335 if ok && err == nil {
307336 // Clear username record from the cache
308337 auth .ClearPassword (username )
338+ // Clear user record from the user cache
339+ auth .RemoveCredentialFromCache (username )
309340 msg := fmt .Sprintf (`user with "username"="%s" deleted` , username )
310341 util .WriteBackMessage (w , msg , http .StatusOK )
311342 return
@@ -330,6 +361,8 @@ func (u *Users) deleteUserWithUsername() http.HandlerFunc {
330361 if ok && err == nil {
331362 // Clear username record from the cache
332363 auth .ClearPassword (username )
364+ // Clear user record from the user cache
365+ auth .RemoveCredentialFromCache (username )
333366 msg := fmt .Sprintf (`user with "username"="%s" deleted` , username )
334367 util .WriteBackMessage (w , msg , http .StatusOK )
335368 return
0 commit comments