added fallback middleware
All checks were successful
Release / publish (push) Successful in 4m7s

This commit is contained in:
u80864958
2025-05-12 09:30:38 +02:00
parent ca99d8fb87
commit 73ff28347a
6 changed files with 161 additions and 8 deletions

View File

@ -50,6 +50,45 @@ func (s *Service) Signup(w http.ResponseWriter, r *http.Request) {
}
}
// Signup handles user signup by decoding request body, hashing the password, and saving user data to the database.
func (s *Service) ChangePassword(w http.ResponseWriter, r *http.Request) {
var err error
var login Login
var password []byte
claims, ok := ExtractClaims(r.Context())
if !ok {
log.Println("Error while extracting claims")
w.WriteHeader(http.StatusInternalServerError)
return
}
if err = json.NewDecoder(r.Body).Decode(&login); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
if len([]byte(login.Password)) > 72 {
fmt.Fprint(w, "Password to long, max 72 bytes")
w.WriteHeader(http.StatusBadRequest)
return
}
if password, err = bcrypt.GenerateFromPassword([]byte(login.Password), 6); err != nil {
log.Println("Error: ", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
err = s.db.Model(&model.User{}).Where("id = ?", claims.UserID).Update("password", password).Error
if err != nil {
log.Printf("Error: %v", err)
w.WriteHeader(http.StatusInternalServerError)
}
w.WriteHeader(http.StatusOK)
}
// Login handles user login by decoding request body, verifying credentials, and returning a JWT token.
func (s *Service) Login(w http.ResponseWriter, r *http.Request) {
var login Login