Laravel::: multiple user ---- part two

Multiple user part two


Working step::
    1)
    Create 04  Middleware(SuperAdminMiddleware,AdminMiddleware,AuthorMiddleware,EditorMiddleware)

     php artisan make:middleware (MiddlewareName)
    ===============================
    
    2)
    Register All  Middleware in                   karnel(SuperAdminMiddleware,AdminMiddleware,AuthorMiddleware,EditorMiddleware)
    
            use App\Http\Middleware\SuperAdminMiddleware;
            
            protected $routeMiddleware = [
           'superadmin'=> SuperadminMiddleware::class;
            ];
    ===============================
    
    3) Redirect if RedirectIfAuthentication write this code for redirect all user there dashboard
        use Illuminate\Support\Facades\Auth;

       if (Auth::guard($guard)->check() && Auth::user()->role->id==1) {
            return redirect('superadmin/dashboard');
        }elseif (Auth::guard($guard)->check() && Auth::user()->role->id==2){
            return redirect('admin/dashboard');
        }elseif (Auth::guard($guard)->check() && Auth::user()->role->id==3){
            return redirect('editor/dashboard');
        }elseif (Auth::guard($guard)->check() && Auth::user()->role->id==4){
            return redirect('author/dashboard');
        }else{
            return $next($request);
        }
    
    4)
    superadminMiddleware and write this code:::
            use Illuminate\Support\Facades\Auth;
            
           if (Auth::check()) {
            if (Auth::user() && Auth::user()->role_id ==1) {
                return $next($request);
            }
                return redirect('/login');
        }
    ===============================

    5)
    AdminMiddleware and write this code:::
            use Illuminate\Support\Facades\Auth;
            
           if (Auth::check()) {
            if (Auth::user() && Auth::user()->role_id <=2) {
                return $next($request);
            }
                return redirect('/login');
        }
    ===============================
        
    6)
    EditorMiddleware and write this code::
            use Illuminate\Support\Facades\Auth;
        
          if (Auth::check()) {
            if (Auth::user() && Auth::user()->role_id <=3) {
                return $next($request);
            }
                return redirect('/login');
        }
    ===============================
        
    7)
    AuthorMiddleware and write this code:::
            use Illuminate\Support\Facades\Auth;
        
          if (Auth::check()) {
            if (Auth::user() && Auth::user()->role_id <=4) {
                return $next($request);
            }
                return redirect('/login');
        }
    ===============================
        
    8)Go to AuthenticatesUsers.php
            replace this path----
            $this->guard()->user())
             ?: redirect()->intended($this->redirectPath())
            
            --$this->guard()->user())
                ?: $this->CustomRedirectUserPath();
              
            And create this protected function

           protected function CustomRedirectUserPath(){
        if(Auth::user()->role_id==1){
            return redirect('superadmin/dashboard');
        }elseif(Auth::user()->role_id==2){
            return redirect('admin/dashboard');
        }elseif(Auth::user()->role_id==3){
            return redirect('editor/dashboard');
        }elseif(Auth::user()->role_id==4){
            return redirect('author/dashboard');
        }
       }
          
    ===============================
    
    9) write this code contracture of LoginController---
       write this code in construct function

            if(Auth::check() && Auth::user()->role->id==1) {
            $this->redirectTo=route('superadmin.dashboard');
        }elseif(Auth::check() && Auth::user()->role->id==2){
            $this->redirectTo=route('admin.dashboard');      
        }elseif(Auth::check() && Auth::user()->role->id==3){
            $this->redirectTo=route('editor.dashboard');      
        }else{
            $this->redirectTo=route('author.dashboard');      
        }
        $this->middleware('guest')->except('logout');

    ===============================
        
        
    10)
    Route create

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

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

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

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

Comments

  1. Very good & informative content on advertising
    Thanks for sharing such a useful tips.

    Visit our website for Software Development Company in Vadodara

    ReplyDelete

Post a Comment

Popular posts from this blog

Laravel::: Important Link List

Laravel ::::Single Item Searching

Laravel contact form to mail