43 lines
1.2 KiB
Go
43 lines
1.2 KiB
Go
package users
|
|
|
|
import (
|
|
db "ttime/internal/database"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/gofiber/fiber/v2/log"
|
|
"github.com/golang-jwt/jwt/v5"
|
|
)
|
|
|
|
// @Summary UserDelete
|
|
// @Description UserDelete deletes a user from the database
|
|
// @Tags User
|
|
// @Accept json
|
|
// @Produce plain
|
|
// @Security JWT
|
|
// @Success 200 {string} string "User deleted"
|
|
// @Failure 403 {string} string "You can only delete yourself"
|
|
// @Failure 500 {string} string "Internal server error"
|
|
// @Failure 401 {string} string "Unauthorized"
|
|
// @Router /userdelete/{username} [delete]
|
|
//
|
|
// UserDelete deletes a user from the database
|
|
func UserDelete(c *fiber.Ctx) error {
|
|
// Read from path parameters
|
|
username := c.Params("username")
|
|
|
|
// Read username from Locals
|
|
auth_username := c.Locals("user").(*jwt.Token).Claims.(jwt.MapClaims)["name"].(string)
|
|
|
|
if username == auth_username {
|
|
log.Info("User tried to delete itself")
|
|
return c.Status(403).SendString("You can't delete yourself")
|
|
}
|
|
|
|
if err := db.GetDb(c).RemoveUser(username); err != nil {
|
|
log.Warn("Error deleting user:", err)
|
|
return c.Status(500).SendString(err.Error())
|
|
}
|
|
|
|
log.Info("User deleted:", username)
|
|
return c.Status(200).SendString("User deleted")
|
|
}
|