api_key_save.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package ui // import "miniflux.app/v2/internal/ui"
  4. import (
  5. "net/http"
  6. "miniflux.app/v2/internal/http/request"
  7. "miniflux.app/v2/internal/http/response/html"
  8. "miniflux.app/v2/internal/http/route"
  9. "miniflux.app/v2/internal/model"
  10. "miniflux.app/v2/internal/ui/form"
  11. "miniflux.app/v2/internal/ui/session"
  12. "miniflux.app/v2/internal/ui/view"
  13. "miniflux.app/v2/internal/validator"
  14. )
  15. func (h *handler) saveAPIKey(w http.ResponseWriter, r *http.Request) {
  16. user, err := h.store.UserByID(request.UserID(r))
  17. if err != nil {
  18. html.ServerError(w, r, err)
  19. return
  20. }
  21. apiKeyForm := form.NewAPIKeyForm(r)
  22. apiKeyCreationRequest := &model.APIKeyCreationRequest{
  23. Description: apiKeyForm.Description,
  24. }
  25. if validationErr := validator.ValidateAPIKeyCreation(h.store, user.ID, apiKeyCreationRequest); validationErr != nil {
  26. sess := session.New(h.store, request.SessionID(r))
  27. view := view.New(h.tpl, r, sess)
  28. view.Set("form", apiKeyForm)
  29. view.Set("menu", "settings")
  30. view.Set("user", user)
  31. view.Set("countUnread", h.store.CountUnreadEntries(user.ID))
  32. view.Set("countErrorFeeds", h.store.CountUserFeedsWithErrors(user.ID))
  33. view.Set("errorMessage", validationErr.Translate(user.Language))
  34. html.OK(w, r, view.Render("create_api_key"))
  35. return
  36. }
  37. if _, err = h.store.CreateAPIKey(user.ID, apiKeyCreationRequest.Description); err != nil {
  38. html.ServerError(w, r, err)
  39. return
  40. }
  41. html.Redirect(w, r, route.Path(h.router, "apiKeys"))
  42. }