From b1b6156eac18648e5b932a4c9ce9847924080ec2 Mon Sep 17 00:00:00 2001 From: zztrans <2438362589@qq.com> Date: Tue, 10 Sep 2024 22:23:39 +0800 Subject: [PATCH] feat: logout clear session --- cmd/web_server/handler/auth.go | 2 +- cmd/web_server/handler/user.go | 17 +++++++++++++++++ cmd/web_server/main.go | 4 ++-- docker-compose.yml | 2 +- modules/auth/login_session.go | 9 +++++++++ modules/auth/redis.go | 11 +++++++++++ 6 files changed, 41 insertions(+), 4 deletions(-) diff --git a/cmd/web_server/handler/auth.go b/cmd/web_server/handler/auth.go index ee20932..04bf3c5 100644 --- a/cmd/web_server/handler/auth.go +++ b/cmd/web_server/handler/auth.go @@ -87,7 +87,7 @@ type loginBody struct { // @Tags user // @Accept json // @Param loginBody body loginBody true "body" -// @Router /user/login [post] +// @Router /auth/password [post] // @Success 200 func loginByPassword(ginCtx *gin.Context) { body := &loginBody{} diff --git a/cmd/web_server/handler/user.go b/cmd/web_server/handler/user.go index 469432c..791c28d 100644 --- a/cmd/web_server/handler/user.go +++ b/cmd/web_server/handler/user.go @@ -32,6 +32,7 @@ func SetupUserRouter(baseRoute *gin.RouterGroup) { middleware.BuildCasbinEnforceHandlerWithDomain("system"), revokeUserRole, ) + g.POST("/logout", logout) } } @@ -145,3 +146,19 @@ func revokeUserRole(ginCtx *gin.Context) { ginCtx.Status(http.StatusOK) } + +func logout(ginCtx *gin.Context) { + ls, err := middleware.GetLoginSessionFromGinCtx(ginCtx) + if err != nil { + gin_utils.NewUnauthorizedError(ginCtx, "cannot load login session from cookie") + return + } + + err = ls.DelInRedis(ginCtx) + if err != nil { + gin_utils.NewInternalError(ginCtx, fmt.Sprintf("failed to Del session in redis: %v", err)) + return + } + + ginCtx.Status(http.StatusOK) +} diff --git a/cmd/web_server/main.go b/cmd/web_server/main.go index e9cb80d..dfb6750 100644 --- a/cmd/web_server/main.go +++ b/cmd/web_server/main.go @@ -70,10 +70,10 @@ func main() { log_module.AppLogger().Info("Serving swagger Doc...") handler.SetupSwaggoRouter(baseRouter) } - // handler.SetupOauthRouter(baseRouter) + handler.SetupOauthRouter(baseRouter) + apiRouter := r.Group("/api/v1") handler.SetupUserRouter(apiRouter) - handler.SetupOauthRouter(apiRouter) handler.SetupProblemRouter(apiRouter) handler.SetupEventRouter(apiRouter) handler.SetupJudgeRouter(apiRouter) diff --git a/docker-compose.yml b/docker-compose.yml index ee89a0e..6c38e1c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -54,7 +54,7 @@ services: image: ghcr.io/oj-lab/judger:main pull_policy: always extra_hosts: - - "host.docker.internal:host-gateway" + - "host.docker.internal:172.24.95.162" environment: - ENABLE_RCLONE=true ports: diff --git a/modules/auth/login_session.go b/modules/auth/login_session.go index 476357f..680ec4d 100644 --- a/modules/auth/login_session.go +++ b/modules/auth/login_session.go @@ -57,3 +57,12 @@ func (ls LoginSession) SaveToRedis(ctx context.Context) error { return nil } + +func (ls LoginSession) DelInRedis(ctx context.Context) error { + err := DelLoginSession(ctx, ls.Key) + if err != nil { + return err + } + + return nil +} diff --git a/modules/auth/redis.go b/modules/auth/redis.go index c58ca7c..d1fdfc0 100644 --- a/modules/auth/redis.go +++ b/modules/auth/redis.go @@ -33,6 +33,17 @@ func SetLoginSession(ctx context.Context, key LoginSessionKey, data LoginSession return nil } +func DelLoginSession(ctx context.Context, key LoginSessionKey) error { + redisClient := redis_agent.GetDefaultRedisClient() + + err := redisClient.Del(ctx, getLoginSessionRedisKey(key)).Err() + if err != nil { + return err + } + + return nil +} + func GetLoginSession(ctx context.Context, key LoginSessionKey) (*LoginSession, error) { redisClient := redis_agent.GetDefaultRedisClient()