Refactor middleware methods and implement cookie middlewares.