1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-09-15 18:57:04 +00:00

Handle the case when application session is expired and not user session

This commit is contained in:
Frédéric Guillot 2019-01-21 20:21:05 -08:00
parent 6378ad2734
commit 7897d8a8ad
5 changed files with 65 additions and 32 deletions

View file

@ -61,12 +61,21 @@ func (m *middleware) handleAppSession(next http.Handler) http.Handler {
session := m.getAppSessionValueFromCookie(r)
if session == nil {
logger.Debug("[UI:AppSession] Session not found")
session, err = m.store.CreateSession()
if err != nil {
html.ServerError(w, r, err)
return
if (request.IsAuthenticated(r)) {
userID := request.UserID(r)
logger.Debug("[UI:AppSession] Cookie expired but user #%d is logged: creating a new session", userID)
session, err = m.store.CreateAppSessionWithUserPrefs(userID)
if err != nil {
html.ServerError(w, r, err)
return
}
} else {
logger.Debug("[UI:AppSession] Session not found, creating a new one")
session, err = m.store.CreateAppSession()
if err != nil {
html.ServerError(w, r, err)
return
}
}
http.SetCookie(w, cookie.New(cookie.CookieSessionID, session.ID, m.cfg.IsHTTPS, m.cfg.BasePath()))
@ -104,7 +113,7 @@ func (m *middleware) getAppSessionValueFromCookie(r *http.Request) *model.Sessio
return nil
}
session, err := m.store.Session(cookieValue)
session, err := m.store.AppSession(cookieValue)
if err != nil {
logger.Error("[UI:AppSession] %v", err)
return nil

View file

@ -18,49 +18,49 @@ type Session struct {
// 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.sessionID, "oauth2_state", state)
s.store.UpdateAppSessionField(s.sessionID, "oauth2_state", state)
return state
}
// NewFlashMessage creates a new flash message.
func (s *Session) NewFlashMessage(message string) {
s.store.UpdateSessionField(s.sessionID, "flash_message", message)
s.store.UpdateAppSessionField(s.sessionID, "flash_message", message)
}
// FlashMessage returns the current flash message if any.
func (s *Session) FlashMessage(message string) string {
if message != "" {
s.store.UpdateSessionField(s.sessionID, "flash_message", "")
s.store.UpdateAppSessionField(s.sessionID, "flash_message", "")
}
return message
}
// NewFlashErrorMessage creates a new flash error message.
func (s *Session) NewFlashErrorMessage(message string) {
s.store.UpdateSessionField(s.sessionID, "flash_error_message", message)
s.store.UpdateAppSessionField(s.sessionID, "flash_error_message", message)
}
// FlashErrorMessage returns the last flash error message if any.
func (s *Session) FlashErrorMessage(message string) string {
if message != "" {
s.store.UpdateSessionField(s.sessionID, "flash_error_message", "")
s.store.UpdateAppSessionField(s.sessionID, "flash_error_message", "")
}
return message
}
// SetLanguage updates the language field in session.
func (s *Session) SetLanguage(language string) {
s.store.UpdateSessionField(s.sessionID, "language", language)
s.store.UpdateAppSessionField(s.sessionID, "language", language)
}
// SetTheme updates the theme field in session.
func (s *Session) SetTheme(theme string) {
s.store.UpdateSessionField(s.sessionID, "theme", theme)
s.store.UpdateAppSessionField(s.sessionID, "theme", theme)
}
// SetPocketRequestToken updates Pocket Request Token.
func (s *Session) SetPocketRequestToken(requestToken string) {
s.store.UpdateSessionField(s.sessionID, "pocket_request_token", requestToken)
s.store.UpdateAppSessionField(s.sessionID, "pocket_request_token", requestToken)
}
// New returns a new session handler.

View file

@ -22,8 +22,8 @@ func Serve(router *mux.Router, cfg *config.Config, store *storage.Storage, pool
handler := &handler{router, cfg, store, template.NewEngine(cfg, router), pool, feedHandler}
uiRouter := router.NewRoute().Subrouter()
uiRouter.Use(middleware.handleAppSession)
uiRouter.Use(middleware.handleUserSession)
uiRouter.Use(middleware.handleAppSession)
// Static assets.
uiRouter.HandleFunc("/stylesheets/{name}.css", handler.showStylesheet).Name("stylesheet").Methods("GET")