Laravel Middleware sorting
At rare times, according to the documentation, you want to control the order in which middleware is run. Middleware is sorted to make sure that encryption/decryption of the session runs before the session starts, which runs before authentication, which runs before authenticating the session, etc. By default, all your application middleware is run after all system middleware, following the default order in Illuminate\Foundation\Http\Kernel::$middlewarePriority
As can be seen from that default property, some middleware is included using a contract. The sorting mechanism only determines if a given middleware is implementing that class or contract, so you can adjust your own middleware to implement one of these classes and contracts to have it sorted at that point in the sequence.
In my use case, I needed to run an additional authentication method before the general authenticated-check on my route. So I created a new middleware, had it implement the \Illuminate\Contracts\Auth\Middleware\AuthenticatesRequests::class
(which is empty) and made sure it got called before ‘auth’ in the middleware specification of my route. In that way it is run just before the authentication is checked and the Laravel default middleware correctly determines the user is now logged in.