session.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
  2. // SPDX-License-Identifier: Apache-2.0
  3. package session // import "miniflux.app/v2/internal/ui/session"
  4. import (
  5. "time"
  6. "miniflux.app/v2/internal/model"
  7. "miniflux.app/v2/internal/storage"
  8. )
  9. // Session handles session data.
  10. type Session struct {
  11. store *storage.Storage
  12. sessionID string
  13. }
  14. // New returns a new session handler.
  15. func New(store *storage.Storage, sessionID string) *Session {
  16. return &Session{store, sessionID}
  17. }
  18. func (s *Session) SetLastForceRefresh() {
  19. s.store.SetAppSessionTextField(s.sessionID, "last_force_refresh", time.Now().UTC().Unix())
  20. }
  21. func (s *Session) SetOAuth2State(state string) {
  22. s.store.SetAppSessionTextField(s.sessionID, "oauth2_state", state)
  23. }
  24. func (s *Session) SetOAuth2CodeVerifier(codeVerfier string) {
  25. s.store.SetAppSessionTextField(s.sessionID, "oauth2_code_verifier", codeVerfier)
  26. }
  27. // NewFlashMessage creates a new flash message.
  28. func (s *Session) NewFlashMessage(message string) {
  29. s.store.SetAppSessionTextField(s.sessionID, "flash_message", message)
  30. }
  31. // FlashMessage returns the current flash message if any.
  32. func (s *Session) FlashMessage(message string) string {
  33. if message != "" {
  34. s.store.SetAppSessionTextField(s.sessionID, "flash_message", "")
  35. }
  36. return message
  37. }
  38. // NewFlashErrorMessage creates a new flash error message.
  39. func (s *Session) NewFlashErrorMessage(message string) {
  40. s.store.SetAppSessionTextField(s.sessionID, "flash_error_message", message)
  41. }
  42. // FlashErrorMessage returns the last flash error message if any.
  43. func (s *Session) FlashErrorMessage(message string) string {
  44. if message != "" {
  45. s.store.SetAppSessionTextField(s.sessionID, "flash_error_message", "")
  46. }
  47. return message
  48. }
  49. // SetLanguage updates the language field in session.
  50. func (s *Session) SetLanguage(language string) {
  51. s.store.SetAppSessionTextField(s.sessionID, "language", language)
  52. }
  53. // SetTheme updates the theme field in session.
  54. func (s *Session) SetTheme(theme string) {
  55. s.store.SetAppSessionTextField(s.sessionID, "theme", theme)
  56. }
  57. // SetWebAuthnSessionData sets the WebAuthn session data.
  58. func (s *Session) SetWebAuthnSessionData(sessionData *model.WebAuthnSession) {
  59. s.store.SetAppSessionJSONField(s.sessionID, "webauthn_session_data", sessionData)
  60. }