Laravel 9 Livewire Breeze authentication

PHOTO EMBED

Tue Jan 03 2023 07:18:36 GMT+0000 (Coordinated Universal Time)

Saved by @ahmad007 #laravel #ajax

rename the app.blade.php file if exists for temporary
1. composer require laravel/breeze --dev
2. php artisan breeze:install
Delete the new built app.blade.php file and change the name of the file file in step 1 to again app.blade.php
Now add the following code in users file migration after password
  $table->string('utype')->default('USR')->comment('ADM for admin and USR for User');
3. now run php artisan migrate
4. npm install
5. npm run build
6. Now we can change or use login/logout/Register routes routes available 

 @auth
    <ul>                                
      <li><i class="fi-rs-key"></i>  {{Auth::user()->name}}  / 
        <form method="POST" action="{{route('logout')}}">
          @csrf
          <a href="{{route('logout')}}" onclick="event.preventDefault(); 			    					this.closest('form').submit();">Logout</a>
        </form>
       </li>
    </ul>
 @else
     <ul>                                
       <li><i class="fi-rs-key"></i>
		  <a href="{{route('login')}}">Log In </a>  / <a href="{{route('register')}}">Sign Up</a>		</li>
     </ul>
 @endif

7. the make middleware for that
php artisan make:middleware AdminMiddleware
8. then open the the middle file in http/middleware
9. now add the following code inhandle function

if (Auth::user()->utype === 'ADM') {
   return $next($request);
 }
 else{
 session()->flush();
 return redirect()->route('login');
 }
10. Use the following code in the header of middleware file to use auth
use Illuminate\Support\Facades\Auth;
11. Now open kernel.php file in http folder and add the following code in routemiddleware
'authadmin' => \App\Http\Middleware\AdminMiddleware::class,
  12. Now open the file routeservice provider in providers folder and delete the dashboard word in the line no 20 HOME
13. Now make two componnts use the following commands
php artisan make:livewire Admin/AdminDashboardComponent
php artisan make:livewire User/UserDashboardComponent
14. Now use the following code in web.php to use middleware in your project
Route::middleware(['auth'])->group(function(){
    Route::get('/user/dashboard', UserDashboardComponent::class)->name('user.dashboard');
});

Route::middleware(['auth', 'authadmin'])->group(function(){
    Route::get('/admin/dashboard', AdminDashboardComponent::class)->name('admin.dashboard');
});
content_copyCOPY