The backend for the project formerly known as signet, now known as beignet.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

changepassword.go 986 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package endpoints
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "time"
  6. . "github.com/imosed/signet/data"
  7. )
  8. type ChangePasswordRequest struct {
  9. UserID uint `json:"userID"`
  10. Password string `json:"password"`
  11. }
  12. func ChangePassword(w http.ResponseWriter, r *http.Request) {
  13. var req ChangePasswordRequest
  14. err := json.NewDecoder(r.Body).Decode(&req)
  15. if err != nil {
  16. panic("Could not decode body")
  17. }
  18. var user User
  19. Db.Table("users").First(&user, req.UserID)
  20. var password string
  21. password, err = GetHashedPassword(req.Password)
  22. if err != nil {
  23. panic("Could not get password")
  24. }
  25. if user.LastLogin == nil {
  26. Db.Table("users").Where("id = ?", req.UserID).Updates(map[string]interface{}{"last_login": time.Now(), "password": password})
  27. } else {
  28. Db.Table("users").Where("id = ?", req.UserID).Update("password = ?", password)
  29. }
  30. var resp SuccessResponse
  31. resp.Success = true
  32. err = json.NewEncoder(w).Encode(resp)
  33. if err != nil {
  34. panic("Could not deliver response")
  35. }
  36. }