Skip to content

πŸ“š The open-source, offline-first Notion, Obsidian & Confluence alternative. Advanced Markdown, multi-tenant teams, OIDC/SSO, and local S3 backups. Deploy in one command.

Implement logout from all devices in backend

Smaug6739d33cc5e14d04
diff --git a/backend/controllers/authentication.controller.go b/backend/controllers/authentication.controller.go
index 90d6b9fd..8e78c485 100644
--- a/backend/controllers/authentication.controller.go
+++ b/backend/controllers/authentication.controller.go
@@ -4,6 +4,7 @@ import (
"alexandrie/app"
"alexandrie/models"
"alexandrie/types"
+ "alexandrie/utils"
"crypto/rand"
"errors"
"fmt"
@@ -24,6 +25,7 @@ type AuthController interface {
RequestResetPassword(c *gin.Context) (int, any)
ResetPassword(c *gin.Context) (int, any)
Logout(c *gin.Context) (int, any)
+ LogoutAllDevices(c *gin.Context) (int, any)
}
func NewAuthController(app *app.App) AuthController {
@@ -173,6 +175,28 @@ func (ctr *Controller) Logout(c *gin.Context) (int, any) {
return http.StatusOK, "Logged out successfully."
}
+// Logout all devices
+// @Summary Logout all devices
+// @Method POST
+// @Router /auth/logout/all [post]
+// @Security Session validation
+// @Success 200 {object} Success(string)
+// @Failure 400 {object} Error
+func (ctr *Controller) LogoutAllDevices(c *gin.Context) (int, any) {
+ // Get the user ID from the context
+ userId, err := utils.GetUserIdCtx(c)
+ if err != nil {
+ return http.StatusBadRequest, err
+ }
+
+ if err := ctr.app.Services.Session.DeleteAllUserSessions(userId); err != nil {
+ return http.StatusInternalServerError, errors.New("failed to delete sessions")
+ }
+ c.SetCookie("Authorization", "", -1, "/", os.Getenv("COOKIE_DOMAIN"), true, true)
+ c.SetCookie("RefreshToken", "", -1, "/", os.Getenv("COOKIE_DOMAIN"), true, true)
+ return http.StatusOK, "Logged out from all devices successfully."
+}
+
// Reset password
// @Summary Reset password
// @Method POST
diff --git a/backend/router/routes/auth.go b/backend/router/routes/auth.go
index 8b40c042..e2c093c5 100644
--- a/backend/router/routes/auth.go
+++ b/backend/router/routes/auth.go
@@ -3,6 +3,7 @@ package routes
import (
"alexandrie/app"
"alexandrie/controllers"
+ "alexandrie/middlewares"
"alexandrie/utils"
"github.com/gin-gonic/gin"
@@ -16,5 +17,6 @@ func Auth(app *app.App, router *gin.RouterGroup) {
auth.POST("/refresh", utils.WP(authCtrl.RefreshSession))
auth.POST("/request-reset", utils.WP(authCtrl.RequestResetPassword))
auth.POST("/reset-password", utils.WP(authCtrl.ResetPassword))
- auth.POST("/logout", utils.WP(authCtrl.Logout))
+ auth.POST("/logout", middlewares.Auth(), utils.WP(authCtrl.Logout))
+ auth.POST("/logout/all", middlewares.Auth(), utils.WP(authCtrl.LogoutAllDevices))
}
diff --git a/backend/services/session.service.go b/backend/services/session.service.go
index 5cfab443..ac2b07ab 100644
--- a/backend/services/session.service.go
+++ b/backend/services/session.service.go
@@ -12,6 +12,7 @@ type AuthService interface {
GetSession(refreshToken string) (models.Session, error)
UpdateSession(session *models.Session) (*models.Session, error)
DeleteSession(id types.Snowflake) error
+ DeleteAllUserSessions(userId types.Snowflake) error
DeleteOldSessions() error
}
@@ -53,6 +54,14 @@ func (s *Service) DeleteSession(id types.Snowflake) error {
return nil
}
+func (s *Service) DeleteAllUserSessions(userId types.Snowflake) error {
+ _, err := s.db.Exec("DELETE FROM sessions WHERE user_id = ?", userId)
+ 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 {