The backend for the project formerly known as signet, now known as beignet.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

46 rader
986 B

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