Support the ongoing development of Laravel.io →
Authentication
Last updated 2 years ago.
0

you can make the changes on the RedirectIfAuthenticated middleware

0

I was doing the same thing in 5.2 and am trying to migrate to 5.3. It looks like the proper way to handle it is to do it in the App\Exceptions\Handler::unauthenticated() function, except I don't see how to retrieve the name of the guard to set the redirect URL.

What I ended up doing is retrieve the middleware from the route and parse what auth middleware is on the route. This wouldn't work if you have multiple guards on the middleware, but I can't figure out a better way.


protected function unauthenticated($request, AuthenticationException $exception)
{
    if ($request->expectsJson()) {
        return response()->json(['error' => 'Unauthenticated.'], 401);
    }

    // Customize the redirect based on the guard
    // Note that we don't know which guard failed here, but I can't find an elegant way
    // to handle this and I know in this project I am only using one guard at a time anyway.
    $middleware = request()->route()->gatherMiddleware();
    $guard = config('auth.defaults.guard');
    foreach($middleware as $m) {
        if(preg_match("/auth:/",$m)) {
            list($mid, $guard) = explode(":",$m);
        }
    }

    switch($guard) {
        case 'group-leader':
            $login = 'groups/auth/login';
            break;
        case 'camper':
            $login = 'campers/portal/login';
            break;
        case 'admin':
            $login = 'admin/login';
            break;
        default:
            $login = '/';
            break;
    }

    return redirect()->guest($login);
}

0

Thanks a bunch @jdavidbakr, was looking exactly for this :)

0

Hi,

Im new to laravel 5.3.

How do I customize the guard? and where should I edit it?

Thanks.

0

What's actual problem behind this ? I only get this problem with Nginx but everything runs OK in apache

0

i'm pretty sure you can do this in RedirectIfAuthenticated middleware

0

in app/exceptions/handler.php

    protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'Unauthenticated.'], 401);
        }

        $guard = array_get($exception->guards(), 0);

        switch ($guard) {
            case 'backend':
                $login = 'backend/login';
                break;
            default:
                $login = 'login';
                break;
        }

        return redirect()->guest($login);
    }
0

this should work!

ternavsky said:

in app/exceptions/handler.php

   protected function unauthenticated($request, AuthenticationException $exception)
   {
       if ($request->expectsJson()) {
           return response()->json(['error' => 'Unauthenticated.'], 401);
       }

       $guard = array_get($exception->guards(), 0);

       switch ($guard) {
           case 'backend':
               $login = 'backend/login';
               break;
           default:
               $login = 'login';
               break;
       }

       return redirect()->guest($login);
   }
0

Hi, I am using the same method for multiple users. I have frontend controller for the public access. I want to check there if the user is logged in. If so I want to hide Login button and show home link which should redirect users to his dashboard. How can I check this in front-end where no login is required? Any help is appreciated.?

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.