add logout api

This commit is contained in:
Jay 2018-04-18 15:05:37 +08:00
parent 6419162471
commit d86e8e47f4
3 changed files with 41 additions and 0 deletions

View File

@ -64,3 +64,25 @@ func RedisGet(namespace, key string) (string, error) {
val, err := redisStore.Get(key).Result() val, err := redisStore.Get(key).Result()
return val, err 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
}

View File

@ -95,3 +95,21 @@ func UserLogin(c *context.Context) {
c.Success(m) 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)
}

View File

@ -40,5 +40,6 @@ func SetDefaultRoutes(r *gin.Engine) {
accountAPI := api.Group("/account") accountAPI := api.Group("/account")
{ {
accountAPI.POST("/login", context.PatchContext(account.UserLogin)) accountAPI.POST("/login", context.PatchContext(account.UserLogin))
accountAPI.POST("/logout", context.PatchContext(account.UserLogout))
} }
} }