add logout api
This commit is contained in:
		
							parent
							
								
									6419162471
								
							
						
					
					
						commit
						d86e8e47f4
					
				@ -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
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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))
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user