-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchangePassword.go
69 lines (48 loc) · 1.54 KB
/
changePassword.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package views
import (
"fmt"
"net/http"
"github.com/labstack/echo/v4"
"gopkg.in/guregu/null.v4"
)
// ChangePasswordFunc handles the password change from a user
func (v *Views) ChangePasswordFunc(c echo.Context) error {
if c.Request().Method == http.MethodPost {
c1 := v.getSessionData(c)
err := c.Request().ParseForm()
if err != nil {
return fmt.Errorf("failed to parse form for changePassword: %w", err)
}
oldPassword := c.Request().FormValue("oldPassword")
var message struct {
Message string `json:"message"`
Error string `json:"error"`
}
var status int
c1.User.Password = null.StringFrom(oldPassword)
_, _, err = v.user.VerifyUser(c.Request().Context(), c1.User)
if err != nil {
message.Error = "old password is not correct"
return c.JSON(status, message)
}
password := c.Request().FormValue("newPassword")
errString := minRequirementsMet(password)
if len(errString) > 0 {
message.Error = "new password doesn't meet the old requirements: " + errString
return c.JSON(status, message)
}
if password != c.Request().FormValue("confirmationPassword") {
message.Error = "new passwords doesn't match"
return c.JSON(status, message)
}
c1.User.Password = null.StringFrom(password)
err = v.user.EditUserPassword(c.Request().Context(), c1.User)
if err != nil {
message.Error = fmt.Sprintf("failed to change password: %+v", err)
return c.JSON(status, message)
}
message.Message = "successfully changed password"
return c.JSON(status, message)
}
return v.invalidMethodUsed(c)
}