|
- package endpoints
-
- import (
- "encoding/json"
- "net/http"
- "time"
-
- . "github.com/imosed/signet/data"
- )
-
- type ChangePasswordRequest struct {
- UserID uint `json:"userID"`
- Password string `json:"password"`
- }
-
- func ChangePassword(w http.ResponseWriter, r *http.Request) {
- var req ChangePasswordRequest
- err := json.NewDecoder(r.Body).Decode(&req)
- if err != nil {
- panic("Could not decode body")
- }
-
- var user User
- Db.Table("users").First(&user, req.UserID)
-
- var password string
- password, err = GetHashedPassword(req.Password)
- if err != nil {
- panic("Could not get password")
- }
-
- if user.LastLogin == nil {
- Db.Table("users").Where("id = ?", req.UserID).Updates(map[string]interface{}{"last_login": time.Now(), "password": password})
- } else {
- Db.Table("users").Where("id = ?", req.UserID).Update("password = ?", password)
- }
-
- var resp SuccessResponse
- resp.Success = true
-
- err = json.NewEncoder(w).Encode(resp)
- if err != nil {
- panic("Could not deliver response")
- }
- }
|