Yes, it is possible to remove middleware based on a session key. You can modify the middleware stack dynamically based on the session value. Here's an example:
public function __construct()
{
$this->middleware(function ($request, $next) {
if (Session::has('imperson_by')) {
$this->middleware('middleware_name', ['except' => ['route_name']]);
}
return $next($request);
});
}
In this example, we are checking if the session key 'imperson_by' exists. If it does, we are removing the middleware named 'middleware_name' from the middleware stack for all routes except for the route named 'route_name'. You can modify this to suit your specific needs.
Note that this code should be placed in the constructor of your controller or middleware.