aboutsummaryrefslogtreecommitdiffhomepage
path: root/ui/session/session.go
blob: fc25406b370f54cbc3ec7c2d08362c49150eca75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// Copyright 2018 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.

package session

import (
	"github.com/miniflux/miniflux/crypto"
	"github.com/miniflux/miniflux/http/context"
	"github.com/miniflux/miniflux/storage"
)

// Session handles session data.
type Session struct {
	store *storage.Storage
	ctx   *context.Context
}

// NewOAuth2State generates a new OAuth2 state and stores the value into the database.
func (s *Session) NewOAuth2State() string {
	state := crypto.GenerateRandomString(32)
	s.store.UpdateSessionField(s.ctx.SessionID(), "oauth2_state", state)
	return state
}

// NewFlashMessage creates a new flash message.
func (s *Session) NewFlashMessage(message string) {
	s.store.UpdateSessionField(s.ctx.SessionID(), "flash_message", message)
}

// FlashMessage returns the current flash message if any.
func (s *Session) FlashMessage() string {
	message := s.ctx.FlashMessage()
	if message != "" {
		s.store.UpdateSessionField(s.ctx.SessionID(), "flash_message", "")
	}
	return message
}

// NewFlashErrorMessage creates a new flash error message.
func (s *Session) NewFlashErrorMessage(message string) {
	s.store.UpdateSessionField(s.ctx.SessionID(), "flash_error_message", message)
}

// FlashErrorMessage returns the last flash error message if any.
func (s *Session) FlashErrorMessage() string {
	message := s.ctx.FlashErrorMessage()
	if message != "" {
		s.store.UpdateSessionField(s.ctx.SessionID(), "flash_error_message", "")
	}
	return message
}

// SetLanguage updates language field in session.
func (s *Session) SetLanguage(language string) {
	s.store.UpdateSessionField(s.ctx.SessionID(), "language", language)
}

// New returns a new session handler.
func New(store *storage.Storage, ctx *context.Context) *Session {
	return &Session{store, ctx}
}