You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.1 KiB

package Auth
import (
"encoding/json"
"net/http"
"git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Database"
"git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Models"
)
type ChangePassword struct {
Password string `json:"password"`
ConfirmPassword string `json:"confirm_password"`
}
func UpdatePassword(w http.ResponseWriter, r *http.Request) {
var (
changePasswd ChangePassword
userData Models.User
err error
)
userData, err = CheckCookieCurrentUser(w, r)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
err = json.NewDecoder(r.Body).Decode(&changePasswd)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
if changePasswd.Password != changePasswd.ConfirmPassword {
w.WriteHeader(http.StatusBadRequest)
return
}
userData.Password, err = HashPassword(changePasswd.Password)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
err = Database.UpdateUser(userData.ID.String(), &userData)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}