π The open-source, offline-first Notion, Obsidian & Confluence alternative. Advanced Markdown, multi-tenant teams, OIDC/SSO, and local S3 backups. Deploy in one command.
Vue dockergolangkanbanknowledge-baseknowledge-managementmarkdownnote-takingnotesnotes-appnotionnuxtnuxtjsoffline-firstoidcpwaselfhostedssovuevuejswiki
Add logout route & service
Smaug#6739f764a75113e1
diff --git a/backend/controllers/authentication.controller.go b/backend/controllers/authentication.controller.goindex 6e1f6f7a..3055d701 100644--- a/backend/controllers/authentication.controller.go+++ b/backend/controllers/authentication.controller.go@@ -19,6 +19,7 @@ import (type AuthController interface {Login(c *gin.Context) (int, any)RefreshSession(c *gin.Context) (int, any)+ Logout(c *gin.Context) (int, any)}func NewAuthController(app *app.App) AuthController {@@ -78,7 +79,7 @@ func (ctr *Controller) Login(c *gin.Context) (int, any) {return http.StatusInternalServerError, errors.New("failed to create session")}- c.SetCookie("Authorization", tokenString, 1800, "/", "localhost", false, true)+ c.SetCookie("Authorization", tokenString, ctr.app.Config.Auth.RefreshTokenExpiry, "/", "localhost", false, true)c.SetCookie("RefreshToken", session.RefreshToken, int(time.Duration(ctr.app.Config.Auth.RefreshTokenExpiry).Seconds()), "/", "localhost", false, true)user.Password = ""@@ -140,6 +141,34 @@ func (ctr *Controller) RefreshSession(c *gin.Context) (int, any) {return http.StatusOK, "Session refreshed successfully."}+// Logout+// @Summary Logout+// @Method POST+// @Router /auth/logout [post]+// @Security Session validation+// @Success 200 {object} Success(string)+// @Failure 400 {object} Error+func (ctr *Controller) Logout(c *gin.Context) (int, any) {+ // Get the refresh token from the cookie+ refreshToken, err := c.Cookie("RefreshToken")+ if err != nil {+ return http.StatusUnauthorized, errors.New("no refresh token provided")+ }+ session, err := ctr.app.Services.Session.GetSession(refreshToken)+ if err != nil {+ return http.StatusUnauthorized, errors.New("invalid refresh token")+ }+ if session.ExpireToken < time.Now().UnixMilli() {+ return http.StatusUnauthorized, errors.New("refresh token expired")+ }+ if err = ctr.app.Services.Session.DeleteSession(session.Id); err != nil {+ return http.StatusInternalServerError, errors.New("failed to delete session")+ }+ c.SetCookie("Authorization", "", -1, "/", "localhost", false, true)+ c.SetCookie("RefreshToken", "", -1, "/", "localhost", false, true)+ return http.StatusOK, "Logged out successfully."+}+func (ctr *Controller) signAccessToken(user *models.User) (string, error) {claims := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{"sub": strconv.FormatInt(user.Id, 10), // Subject (user identifier)diff --git a/backend/router/routes/auth.go b/backend/router/routes/auth.goindex 50964192..ac5de728 100644--- a/backend/router/routes/auth.go+++ b/backend/router/routes/auth.go@@ -14,4 +14,5 @@ func Auth(app *app.App, router *gin.RouterGroup) {authCtrl := controllers.NewAuthController(app)auth.POST("/", utils.WP(authCtrl.Login))auth.POST("/refresh", utils.WP(authCtrl.RefreshSession))+ auth.POST("/logout", utils.WP(authCtrl.Logout))}diff --git a/backend/services/session.service.go b/backend/services/session.service.goindex 94a156a2..bab4ba17 100644--- a/backend/services/session.service.go+++ b/backend/services/session.service.go@@ -10,6 +10,7 @@ type AuthService interface {CreateSession(session *models.Session) (*models.Session, error)GetSession(refreshToken string) (models.Session, error)UpdateSession(session *models.Session) (*models.Session, error)+ DeleteSession(id int64) errorDeleteOldSessions() error}@@ -43,6 +44,14 @@ func (s *Service) UpdateSession(session *models.Session) (*models.Session, errorreturn session, nil}+func (s *Service) DeleteSession(id int64) error {+ _, err := s.db.Exec("DELETE FROM sessions WHERE id = ?", id)+ if err != nil {+ return err+ }+ return nil+}+func (s *Service) DeleteOldSessions() error {_, err := s.db.Exec("DELETE FROM sessions WHERE expire_token < ? OR active = 0", time.Now().UnixMilli())if err != nil {