Improve frontend auth handling

This commit is contained in:
2026-05-02 15:13:28 +02:00
parent f1fd72520a
commit 36a5911fe4
11 changed files with 126 additions and 47 deletions
+20
View File
@@ -267,3 +267,23 @@ func (h *UserHandler) DeleteUser(c fiber.Ctx) error {
return c.SendStatus(fiber.StatusNoContent)
}
// GetMe returns the currently authenticated user's details
func (h *UserHandler) GetMe(c fiber.Ctx) error {
uid, ok := c.Locals("user_id").(string)
if !ok || uid == "" {
return c.Status(fiber.StatusUnauthorized).JSON(&models.APIError{Message: "Unauthorized"})
}
user, err := h.Repo.GetUserById(uid)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
return c.Status(fiber.StatusNotFound).JSON(&models.APIError{Message: "User not found"})
}
log.Println("Error getting user: ", err)
return c.Status(fiber.StatusInternalServerError).JSON(&models.APIError{Message: "Unexpected error while getting user"})
}
user.HashedPassword = ""
return c.JSON(user)
}