From d86e8e47f49e0bfd495b6550fdc74bf5616ca60c Mon Sep 17 00:00:00 2001 From: Jay Date: Wed, 18 Apr 2018 15:05:37 +0800 Subject: [PATCH] add logout api --- modules/memstore/redis.go | 22 ++++++++++++++++++++++ routers/account/account.go | 18 ++++++++++++++++++ routers/routes/routes.go | 1 + 3 files changed, 41 insertions(+) diff --git a/modules/memstore/redis.go b/modules/memstore/redis.go index ab516e7..2aa5693 100644 --- a/modules/memstore/redis.go +++ b/modules/memstore/redis.go @@ -64,3 +64,25 @@ func RedisGet(namespace, key string) (string, error) { val, err := redisStore.Get(key).Result() return val, err } + +// RedisDel - +func RedisDel(namespace, key string) error { + var n string + if len(namespace) > 0 { + n += namespace + n += ":" + } + + if len(key) == 0 { + return errors.New("key empty") + } + + if len(n) > 0 { + n += ":" + } + + n += key + + err := redisStore.Del(n).Err() + return err +} diff --git a/routers/account/account.go b/routers/account/account.go index 0251ed6..2bae102 100644 --- a/routers/account/account.go +++ b/routers/account/account.go @@ -95,3 +95,21 @@ func UserLogin(c *context.Context) { c.Success(m) } + +// UserLogout route +func UserLogout(c *context.Context) { + token := c.GetHeader("X-Auth-Token") + // token, ok := c.C["token"] + if len(token) == 0 { + c.DataFormat("token not found") + return + } + + err := memstore.RedisDel("golang", token) + if err != nil { + c.ServerError("remvoe session fail") + return + } + + c.Success(nil) +} diff --git a/routers/routes/routes.go b/routers/routes/routes.go index 7e39c25..3d1c491 100644 --- a/routers/routes/routes.go +++ b/routers/routes/routes.go @@ -40,5 +40,6 @@ func SetDefaultRoutes(r *gin.Engine) { accountAPI := api.Group("/account") { accountAPI.POST("/login", context.PatchContext(account.UserLogin)) + accountAPI.POST("/logout", context.PatchContext(account.UserLogout)) } }