logout.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package ui
  5. import (
  6. "net/http"
  7. "github.com/miniflux/miniflux/http/context"
  8. "github.com/miniflux/miniflux/http/cookie"
  9. "github.com/miniflux/miniflux/http/response"
  10. "github.com/miniflux/miniflux/http/response/html"
  11. "github.com/miniflux/miniflux/http/route"
  12. "github.com/miniflux/miniflux/logger"
  13. "github.com/miniflux/miniflux/ui/session"
  14. )
  15. // Logout destroy the session and redirects the user to the login page.
  16. func (c *Controller) Logout(w http.ResponseWriter, r *http.Request) {
  17. ctx := context.New(r)
  18. sess := session.New(c.store, ctx)
  19. user, err := c.store.UserByID(ctx.UserID())
  20. if err != nil {
  21. html.ServerError(w, err)
  22. return
  23. }
  24. sess.SetLanguage(user.Language)
  25. if err := c.store.RemoveUserSessionByToken(user.ID, ctx.UserSessionToken()); err != nil {
  26. logger.Error("[Controller:Logout] %v", err)
  27. }
  28. http.SetCookie(w, cookie.Expired(
  29. cookie.CookieUserSessionID,
  30. c.cfg.IsHTTPS,
  31. c.cfg.BasePath(),
  32. ))
  33. response.Redirect(w, r, route.Path(c.router, "login"))
  34. }