add configurable middleware to manage the returnUrl and fix the redirect bug