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

  1. package Auth
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Database"
  6. "git.tovijaeschke.xyz/tovi/SuddenImpactRecords/Models"
  7. )
  8. type ChangePassword struct {
  9. Password string `json:"password"`
  10. ConfirmPassword string `json:"confirm_password"`
  11. }
  12. func UpdatePassword(w http.ResponseWriter, r *http.Request) {
  13. var (
  14. changePasswd ChangePassword
  15. userData Models.User
  16. err error
  17. )
  18. userData, err = CheckCookieCurrentUser(w, r)
  19. if err != nil {
  20. w.WriteHeader(http.StatusUnauthorized)
  21. return
  22. }
  23. err = json.NewDecoder(r.Body).Decode(&changePasswd)
  24. if err != nil {
  25. w.WriteHeader(http.StatusBadRequest)
  26. return
  27. }
  28. if changePasswd.Password != changePasswd.ConfirmPassword {
  29. w.WriteHeader(http.StatusBadRequest)
  30. return
  31. }
  32. userData.Password, err = HashPassword(changePasswd.Password)
  33. if err != nil {
  34. w.WriteHeader(http.StatusInternalServerError)
  35. return
  36. }
  37. err = Database.UpdateUser(userData.ID.String(), &userData)
  38. if err != nil {
  39. w.WriteHeader(http.StatusInternalServerError)
  40. return
  41. }
  42. w.WriteHeader(http.StatusOK)
  43. }