forked from tanitall/memo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
change-password.go
51 lines (46 loc) · 1.29 KB
/
change-password.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
package key
import (
"github.com/jchavannes/jgo/jerr"
"github.com/jchavannes/jgo/web"
"github.com/memocash/memo/app/auth"
"github.com/memocash/memo/app/db"
"github.com/memocash/memo/app/res"
"net/http"
)
var changePasswordRoute = web.Route{
Pattern: res.UrlKeyChangePassword,
NeedsLogin: true,
Handler: func(r *web.Response) {
r.Render()
},
}
var changePasswordSubmitRoute = web.Route{
Pattern: res.UrlKeyChangePasswordSubmit,
CsrfProtect: true,
NeedsLogin: true,
Handler: func(r *web.Response) {
user, err := auth.GetSessionUser(r.Session.CookieId)
if err != nil {
r.Error(jerr.Get("error getting session user", err), http.StatusInternalServerError)
return
}
oldPassword := r.Request.GetFormValue("oldPassword")
newPassword := r.Request.GetFormValue("newPassword")
key, err := db.GetKeyForUser(user.Id)
if err != nil {
r.Error(jerr.Get("error getting key", err), http.StatusInternalServerError)
return
}
err = key.UpdatePassword(oldPassword, newPassword)
if err != nil {
r.Error(jerr.Get("error updating key password", err), http.StatusUnauthorized)
return
}
err = auth.UpdatePassword(user.Id, oldPassword, newPassword)
if err != nil {
r.Error(jerr.Get("error updating user password", err), http.StatusUnauthorized)
return
}
r.Render()
},
}