Using custom middleware redirects me to / home even if Laravel is empty

I am now out of almost any ideas.

The thing is, I have a route for the API endpoint (works fine, responds with JSON, etc.).

If I have now applied the "auth" middleware firmware to the route, I am ALWAYS redirected to the / home route. It looks like I am doing this. wrong with auth? I am wrong because:

This curious redirect also works if I am not using "auth" but custom middleware that contains NOTHING, but

public function handle($request, Closure $next)
{
    print "WTF";
    throw new AuthenticationException('Unauthenticated.');
}

      

[print and exception are never thrown! I will land again without error in / home.]

Not all middleware generates this error: for example, "auth.basic" and "web" just work fine with this route.

I also applied "web" and my own middleware as a route, according to some results I found and said that using "web" solves similar problems for them, but guess what, nothing has changed.

TL: DR: If I am using my own middleware or 'auth', I am redirected, BEFORE AFTER THE SEQUENCE.

Update: After messing around with the code and the large type from mnies, I found this very curious Bug:

If I just uncomment the AuthenticationException , suddenly my code is working as intended. Maybe loading Exception-Class is calling RedirectIfAuthenticated Middleware ?! - which is definitely called!

Easy solution now using a different exception for my custom middleware, but the problem is that by default "auth'-MW" also uses this exception and raises the same Bug.

Remember: I am not using other middleware, not just its own, does the error actually load an Exception like WTF?

So I still need help as to why this is happening!

Error: using

throw new AuthenticationException('Unauthenticated.', []);

      

invokes the "guest" -MW (RedirectIfAuthenticated) prompt instead of the intended MW stack! (nothing from the original MW stack is executed, regardless of order.

Update 2: It seems that RedirectIfAuthenticated is being thrown just because I redirected to the / login route earlier (and from there, as described in / home through it), but that doesn't change the problem that caused this random redirect.

[I am trying to reproduce this error on a fresh install]

Update 3: I was unable to reproduce the error on a fresh installation using Laravel 5.4.19 .... Trying to compare both installations now. D:

Using Laravel 5.3.30.

Some of my code for context:

Route:

Route::get('/admin/user/get', ['middleware' => ['simpleauth', 'web'], 'uses' => 'UserEditAdmin\Controllers\Controller@get']);

      

Custom middleware:

class SimpleAuth
{

    public function __construct(){}

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @return mixed
     * @throws AuthenticationException
     */
    public function handle($request, Closure $next)
    {
        print "WTF";
        throw new AuthenticationException('Unauthenticated.');
    }
}

      

'web' from Kernel.php:

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,

     \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],

    'api' => [
        'throttle:60,1',
        'bindings',
    ],
];

      

+3


source to share


1 answer


Take a look at \App\Http\Kernel.php

. It looks like you always call the middleware \App\Http\Middleware\RedirectIfAuthenticated

(aliased to guest). This is what you want to debug, you can just throw an exception from this middleware to get the stack out of what gets called when.



+1


source







All Articles