Search code examples
laravellaravel-routinglaravel-5.3laravel-authorization

Custom view for authenticated user in laravel 5.3


I'm developing a simple login in laravel 5.3.

I'm having two dashboard in which one is meant for admins and other one for subscribers. I've gone through the documentation of Authentication there is path customization where if I can change the $redirectTo = '/home' to any of the routes it will redirect views respectively.

I've a column in my user table named is_admin which is holding a boolean value.

I'm trying to place following codes in the LoginController according to the documentation:

<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use  \Auth;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
//     protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */

    public function __construct()
    {
        $this->middleware('guest', ['except' => 'logout']);
    }

    /**
     * Handle an authentication attempt.
     *
     * @return Response
     */

    public function redirectPath()
    {      
        if (Auth::user()->is_admin == 0)
        {     

            return redirect()->intended('/memberprofile');   
        }
        else 
        {

          return redirect('/dashboard');      
        }
    } 
}

and commented out protected $redirectTo = '/home'; but it is still redirecting to home. Even if I make protected $redirectTo = '/dashboard' it is routing same to /home. I don't know where problem exist.

I checked the RedirectIfAunthenticated middleware it has following code:

public function handle($request, Closure $next, $guard = null)
{
    if (Auth::guard($guard)->check()) {
        return redirect('/home');
    }
    return $next($request);
}

Is there any problem due to this?

Please guide me


Solution

  • Change handle() method in your app\Http\Middlewares\RedirectIfAunthenticated.php as follow. This middleware is intended to be changed by developers.

    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->check()) {
            if (Auth::user()->is_admin == 0) {
                return redirect()->intended('/memberprofile');   
            } else {
                return redirect('/dashboard');      
            }
        }
    
        return $next($request);
    }