Improve frontend auth handling
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user