Some checks reported errors
arbeitszeitmessung/pipeline/head Something is wrong with the build of this commit
43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package endpoints
|
|
|
|
import (
|
|
"arbeitszeitmessung/models"
|
|
"arbeitszeitmessung/templates"
|
|
"log"
|
|
"net/http"
|
|
)
|
|
|
|
// change user password and store salted hash in db
|
|
func changePassword(w http.ResponseWriter, r *http.Request) {
|
|
err := r.ParseForm()
|
|
if err != nil {
|
|
log.Println("Error parsing form!", err)
|
|
http.Error(w, "Error parsing form error", http.StatusBadRequest)
|
|
return
|
|
}
|
|
password := r.FormValue("password")
|
|
newPassword := r.FormValue("new_password")
|
|
if password == "" || newPassword == "" || newPassword != r.FormValue("new_password_repeat") {
|
|
showUserPage(w, r, http.StatusBadRequest)
|
|
return
|
|
}
|
|
user, err := models.GetUserByPersonalNr(Session.GetInt(r.Context(), "user"))
|
|
if err != nil {
|
|
log.Println("Error getting user!", err)
|
|
showUserPage(w, r, http.StatusBadRequest)
|
|
}
|
|
auth, err := user.ChangePass(password, newPassword)
|
|
if err != nil {
|
|
log.Println("Error when changing password!", err)
|
|
}
|
|
if auth {
|
|
showUserPage(w, r, http.StatusAccepted)
|
|
return
|
|
}
|
|
showUserPage(w, r, http.StatusUnauthorized)
|
|
}
|
|
|
|
func showUserPage(w http.ResponseWriter, r *http.Request, status int) {
|
|
templates.UserPage(status).Render(r.Context(), w)
|
|
}
|