Modified middleware implementation for auth support. Added options processing