4
votes

I am using Laravel project version 5.8.

I have used two middleware Admin and Author.

I have created both AdminMiddleware and AuthorMiddlware.

I registered both in Kernel.php. I changed RedirectIfAuthenticated, loginController for custom redirect. I have created different route groups.

For admin middleware it will redirect admin.dashboard and for author middleware it will redirect to author.dashboard.

After then, it is redirecting to the /home route as in the default laravel logged in users. I want to redirect admin to admin.dashboard and author to author.dashboard after login.Custom redirect is not working. I viewed my project several times but unable to find the issue.

loginController.php

// protected $redirectTo = '/home';
public function __construct(){
    if(Auth::check() && Auth::user()->role->id==1) {
        return redirect()->route('author.dashboard');
    } else if (Auth::check() && Auth::user()->role->id==2) {
        return redirect()->route('admin.dashboard');
    }else {
        $this->middleware('guest')->except('logout');
    }
}

RedirectIfAuthenticated.php

public function handle($request, Closure $next, $guard = null){
    if (Auth::guard($guard)->check() && Auth::user()->role->id==1) {
        return redirect()->route('author.dashboard');
    }else if (Auth::guard($guard)->check() && Auth::user()->role->id==2) {
        return redirect()->route('admin.dashboard');;
    } else {
        return $next($request);
    }
}

AuthorMiddleware

public function handle($request, Closure $next)
{
    if(Auth::check() && Auth::user()->role->id==1){
        return $next($request);
    } else {
        return redirect()->route('/login');
    }
}

AdminMiddleware

public function handle($request, Closure $next)
{
    if(Auth::check() && Auth::user()->role->id==2){
        return $next($request);
    } else {
        return redirect()->route('/login');
    }
}

Web.php

Route::group(['as'=>'author.','prefix'=>'author','namespace'=>'Author','middleware' => 'author'], function () {
Route::get('/dashboard','DashboardController@index')->name('dashboard');
});

Route::group(['as'=>'admin.','prefix'=>'admin','namespace'=>'Admin','middleware' => 'admin'], function () {
Route::get('/dashboard','DashboardController@index')->name('dashboard');
});

Author/DashobardController

public function index()
{
    return view('author.dashboard');
}

Admin/DashobardController

public function index()
{
    return view('admin.dashboard');
}

I am new in Laravel. Faced this problem first time and unable to find issues please help.

2
Do you redirect to /home after login?Immeyti
I want to redirect admin to admin.dashboard and author to author.dashboard after loginMd Rashedul Islam

2 Answers

5
votes

You need to edit the following lines into your LoginController.php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;

use Illuminate\Foundation\Auth\AuthenticatesUsers;

use Illuminate\Http\Request;

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;


protected function authenticated(Request $request, $user)
{
    if(Auth::check() && Auth::user()->role->id==1) {
        return redirect()->route('author.dashboard');
    } else if (Auth::check() && Auth::user()->role->id==2) {
        return redirect()->route('admin.dashboard');
    }

    return redirect('/home');
}
/**
 * Where to redirect users after login.
 *
 * @var string
 */
//protected $redirectTo = '/admin';

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest', ['except' => 'logout']);
}
}
-1
votes

Change

RedirectIfAuthenticated.php

public function handle($request, Closure $next, $guard = null){
    if (Auth::guard($guard)->check() && Auth::user()->role->id==1) {
        return '/author/dashboard';
    }else if (Auth::guard($guard)->check() && Auth::user()->role->id==2) {
        return 'admin/dashboard';
    } else {
        return $next($request);
    }
}

Or

public function handle($request, Closure $next, $guard = null){
    if (Auth::user()->role->id==1) {
        return '/author/dashboard';
    }else if (Auth::user()->role->id==2) {
        return 'admin/dashboard';
    } else {
        return $next($request);
    }
}

Logincontoller.php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;

use Illuminate\Foundation\Auth\AuthenticatesUsers;

use Illuminate\Http\Request;

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;


protected function authenticated(Request $request, $user)
{
    if(Auth::check() && Auth::user()->role->id==1) {
        return 'author/dashboard';
    } else if (Auth::check() && Auth::user()->role->id==2) {
        return 'admin/dashboard';
    }

    return redirect('/home');
}
/**
 * Where to redirect users after login.
 *
 * @var string
 */
//protected $redirectTo = '/admin';

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest', ['except' => 'logout']);
}
}