/
rest_change_pwd.go
44 lines (32 loc) · 1.2 KB
/
rest_change_pwd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package rest
import (
"privateledger/web/model"
"encoding/json"
"net/http"
)
func (app *RestApp) ChangePwdHandler() func(http.ResponseWriter, *http.Request) {
return app.isAuthorized(func(w http.ResponseWriter, r *http.Request) {
orgUser := app.Org.GetOrgUser()
if orgUser == nil {
respondJSON(w, map[string]string{"error": "Error Session User "})
} else {
var userdata model.ModelUserData
_ = json.NewDecoder(r.Body).Decode(&userdata)
email := userdata.Email
role := userdata.Role
oldPwd := hash(userdata.OldPassword)
newPwd := hash(userdata.Password)
verifyErr := verifyPassword(userdata.Password)
if verifyErr != nil && len(verifyErr.Error()) > 0 {
respondJSON(w, map[string]string{"error": verifyErr.Error(), "message": "Password must contain at least one number and one uppercase and lowercase letter, and at least 8 or more characters"})
} else {
err := orgUser.Setup.ChangePassword(email, role, oldPwd, newPwd)
if err != nil {
respondJSON(w, map[string]string{"error": "Unable to Change user pwd - " + err.Error()})
} else {
respondJSON(w, map[string]string{"success": "Password successfully changed for - " + email})
}
}
}
})
}