bmonteiro's avatar

[Laravel 5.3] Change /login auth redirection

Hi,

How can I change the path of /login for log users? In Laravel 5.2, AuthController is present, but in 5.3 is deprecated. My login page is /enter

Insert this in route file is ok, but when i access a restrict page, laravel redirect to /login.

Thanks!

0 likes
4 replies
throttlestudio's avatar
Level 11

@bmonteiro check the Exceptions > Handler.php file

/**
     * Convert an authentication exception into an unauthenticated response.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Illuminate\Auth\AuthenticationException  $exception
     * @return \Illuminate\Http\Response
     */
    protected function unauthenticated($request, AuthenticationException $exception)
    {
        if ($request->expectsJson()) {
            return response()->json(['error' => 'Unauthenticated.'], 401);
        }

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

change guest('login') to your desired location.

17 likes
lgmm's avatar

Also if you have multiple guards you can access those by $exception->guards() method in the exception and redirect to the right login page for the type of user being authenticated.

samim_zp's avatar

how about this one? i want to show /login for normal users and /admin/login for admins and stuffs, so this do the job in my case , I go to myproject/app/http/middleware/RedirectIfAuthenticated.php , and then add something like this to handle function :

        /*
        * if user was not authenticated
        * and try to access any route that contains
        * the prefix /admin,
        * then redirect him to the /admin/login
        */
        if (!Auth::guard($guard)->check() && (strpos($request->session()->get('url')['intended'] , 'admin') == true)) {
            return redirect('/admin/login');
        }

hope it will be helpfull.

2 likes

Please or to participate in this conversation.