brunel/handlers/auth/updatePassword.go

36 lines
818 B
Go
Raw Permalink Normal View History

2024-07-28 20:59:50 +02:00
package handlers_auth
import (
"brunel/auth"
"brunel/helpers"
"github.com/gofiber/fiber/v2"
)
func UpdatePassword(c *fiber.Ctx) error {
username := c.FormValue("username")
password := c.FormValue("password")
passwordConfirm := c.FormValue("passwordConfirm")
user, err := helpers.DBInst.GetUser(username)
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
}
if password != passwordConfirm {
return c.Status(fiber.StatusBadRequest).SendString("Passwords do not match")
}
pw := auth.NewPasswordHash(password)
user.PasswordHash = pw
err = helpers.DBInst.UpdateUser(user)
if err != nil {
return c.Status(fiber.StatusInternalServerError).SendString("Internal Server Error")
}
return c.Status(fiber.StatusOK).SendString("Password updated")
}