Skip to content

Commit

Permalink
feat: logout clear session
Browse files Browse the repository at this point in the history
  • Loading branch information
Zztrans committed Sep 10, 2024
1 parent 238a49d commit b1b6156
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cmd/web_server/handler/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
17 changes: 17 additions & 0 deletions cmd/web_server/handler/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func SetupUserRouter(baseRoute *gin.RouterGroup) {
middleware.BuildCasbinEnforceHandlerWithDomain("system"),
revokeUserRole,
)
g.POST("/logout", logout)
}
}

Expand Down Expand Up @@ -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)
}
4 changes: 2 additions & 2 deletions cmd/web_server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions modules/auth/login_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
11 changes: 11 additions & 0 deletions modules/auth/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit b1b6156

Please sign in to comment.