New Project

  • Laravel 11
    
                                composer create-project --prefer-dist laravel/laravel laravel11-app "11.*"
                                

    OR

    1. clone from https://github.com/savanihd/Laravel-11-Multiple-Authentication-using-Middleware

    2. run composer install

    3. npm install

    4. env

    
                              APP_NAME=Laravel
                              APP_ENV=local
    
                              APP_KEY=base64:plBmzuNAOGiCnxDccxGOUFezoRN6VsZqPubND7hT/Bo=
    
                              APP_DEBUG=true
                              APP_LOG_LEVEL=debug
                              APP_URL=
                              APP_TIMEZONE=Asia/Calcutta
    
                              DB_SETTINGS_IDENTIFIER=SANMAC
    
                              DB_CONNECTION=mysql
                              DB_HOST=127.0.0.1
                              DB_PORT=3306
                              DB_DATABASE=
                              DB_USERNAME=
                              DB_PASSWORD=
    
                              BROADCAST_DRIVER=log
                              CACHE_DRIVER=array
                              SESSION_DRIVER=file
                              QUEUE_DRIVER=sync
    
                              REDIS_HOST=127.0.0.1
                              REDIS_PASSWORD=null
                              REDIS_PORT=6379
    
    
                              MAIL_DRIVER=smtp
                              MAIL_HOST=mail..in
                              MAIL_PORT=587
                              MAIL_USERNAME=mail@.in
                              MAIL_PASSWORD=micr@s@ft123
                              MAIL_ENCRYPTION=
                              MAIL_FROM_ADDRESS=mail@.in
                              MAIL_FROM_NAME=""
    
    
                              RAZOR_PAY_KEY=
                              RAZOR_PAY_SECRET=
    
    
                              VITE_APP_NAME="${APP_NAME}"
    
    
    
    
                              

    5. run in the development mode

    
                              npm run dev
                              

    6. run in production mode

    
                              npm run build
                              
  • Multiple Auth

    1. Route files

    routes/web.php
    
                        use Illuminate\Support\Facades\Route;
    
                        use App\Http\Controllers\Fe\HomeController;
                        use App\Http\Controllers\Auth\AuthController;
    
    
                        
                        Route::get('/', [HomeController::class, 'index'])->name('web.home');
    
    
                        
    routes/admin.php
    
                        use Illuminate\Support\Facades\Route;
    
                        use App\Http\Controllers\HomeController;
                        use App\Http\Controllers\Auth\AuthController;
    
      
                      Route::get('/', [HomeController::class, 'adminHome'])->name('admin.home');
                      Route::get('/login', [AuthController::class, 'admin_login_form'])->name('admin.login');
                      Route::post('/login', [AuthController::class, 'doLogin'])->name('admin.login.submit');
    
                      Route::middleware(['auth', 'user-access:admin'])->group(function () {  
                                                  
                                                  
                                    
                            Route::get('/home', [HomeController::class, 'adminHome'])->name('admin.home');
                            Route::get('/logout', [AuthController::class, 'doLogout'])->name('admin.logout'); 
                            Route::get('/password', [AuthController::class, 'password_form'])->name('admin.password'); 
                            Route::post('/password', [AuthController::class, 'password_save'])->name('admin.password.save'); 
    
    
                            Route::get('/menu',[AdminMenuController::class, 'index'])->name('admin.menu');
                            Route::get('/menu/form',[AdminMenuController::class, 'form'])->name('admin.menu.form');
                            Route::post('/menu/save',[AdminMenuController::class, 'save'])->name('admin.menu.save');     
                            Route::delete('/menu/delete',[AdminMenuController::class, 'delete'])->name('admin.menu.delete');
                            Route::get('/menu/status/{id}',[AdminMenuController::class, 'status'])->name('admin.menu.status');   
                            Route::post('/menu/priority', [AdminMenuController::class, 'priority'])->name('admin.menu.priority');
                            Route::get('/menu/copy',[AdminMenuController::class, 'copy'])->name('admin.menu.copy');
    
                          
           
                      }); 
    
      
    routes/user.php
    
      use Illuminate\Support\Facades\Route;
    
      use App\Http\Controllers\User\HomeController;
      use App\Http\Controllers\Auth\AuthController;
    
    
    
      
      Route::get('/', [HomeController::class, 'index'])->name('user.home');
      Route::get('/login', [AuthController::class, 'fe_login_form'])->name('user.login');
      Route::post('/login', [AuthController::class, 'doLogin'])->name('admin.login.submit');
      Route::middleware(['auth', 'user-access:user'])->group(function () {  
          
          Route::get('/home', [HomeController::class, 'index'])->name('user.home');
          Route::get('/logout', [AuthController::class, 'doLogout'])->name('user.logout'); 
          Route::get('/password', [AuthController::class, 'password_form'])->name('user.password'); 
          Route::post('/password', [AuthController::class, 'password_save'])->name('user.password.save'); 
          
      }); 
    
    

    2. boostrap/app.php

    1. Add then parameter

    2. call the route files admin.php and user.php inside the then()

    3. add redirectGuestsTo()

    
    
    
    use Illuminate\Foundation\Application;
    use Illuminate\Foundation\Configuration\Exceptions;
    use Illuminate\Foundation\Configuration\Middleware;
    use Illuminate\Http\Request;
    
    // use App\Http\Middleware\UserAccess;
    
    return Application::configure(basePath: dirname(__DIR__))
        ->withRouting(
            web: __DIR__.'/../routes/web.php',
            commands: __DIR__.'/../routes/console.php',
            health: '/up',
    
            then: function () {
    
    
                Route::middleware('web')
                    ->group(function () {
                        Route::prefix('user')
                            ->group([
                                __DIR__ . '/../routes/user.php',
                        ]);
                        Route::prefix('admin')
                            ->group([
                                __DIR__ . '/../routes/admin.php',
                        ]);
                    });
    
                    
            },
    
        )
        ->withMiddleware(function (Middleware $middleware) {
            
    
            $middleware->alias([
                'user-access' => \App\Http\Middleware\UserAccess::class,
            ]);
    
    
            $middleware->redirectGuestsTo(function (Request $request) {
                
                if ($request->is('api/*')) {
                    return route('...');
                }  elseif ($request->is('admin')) {
                    return route('admin.login');          
                }
                elseif ($request->is('user')) {
                    return route('user.login');          
                }
                
            });
            
    
        })
        ->withExceptions(function (Exceptions $exceptions) {
           
        })->create();
    
      

    3. AuthController

    1. redirect the route based on auth()->user()->type in the doLogin()

    2. redirect the route based on $user->type in the doLogout()

    
         
      public function admin_login_form(Request $r)
        {
           
            return view('auth.admin.login');
        
        }
    
        public function fe_login_form(Request $r)
        {
           
            return view('auth.fe.login');
        
        }
    
        
        public function doLogin(Request $request): RedirectResponse
        {   
            
            $input = $request->all();
            
            $this->validate($request, [
                'email' => 'required|email',
                'password' => 'required',
            ]);
            
            if(auth()->attempt(array('username' => $input['email'], 'password' => $input['password'])))
            {
                
                
                if (auth()->user()->type == 'admin') {               
                    return redirect()->route('admin.home');
    
                }else if (auth()->user()->type == 'manager') {
                    return redirect()->route('manager.home');
                }else{
                    return redirect()->route('user.home');
                }
                
    
            }else{
                return redirect()->route('login')
                    ->with('error','Email-Address And Password Are Wrong.');
            }
                
        }
    
    
        public function doLogout()
        {
            $user=auth()->user();   
            Auth::logout();   
            
            if ($user->type == 'admin') { 
                return redirect()->route('admin.login');
            }
            else{            
                return redirect()->route('user.login');
            }
        }