|
|
- 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)
- }
|