def __call__(self, req: Request, res: Response, next): now = datetime.now() # try getting session from cookie if self.cookie_id in req.cookies: # TODO check expiration date user_id = req.cookies[self.cookie_id] self.user = self._get_user(user_id) result = next(req, res) if self.user: res.set_cookie(self.cookie_id, str(self.user.id), self.max_age) else: res.set_cookie(self.cookie_id, '', max_age=now.timestamp()) self.user = None return result