In this video we are going to learn about Creating Admin and User Authentication.
So lets see how can we create Admin and User Authentication so first of all lets install the Jetstream package.
Go to the command prompt and just type command.

composer require laravel/Jetstream


Now install the Jetstream livewire package

php artisan jetstream:install livewire

Now run the following npm command

npm install && npm run dev

Now lets user table migration. so go inside the database directory then migrations and from here just open create user table migration.
Inside this migrate add a column for user type. This column define the user whether user is Admin or Normal User.

public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->foreignId('current_team_id')->nullable();
$table->text('profile_photo_path')->nullable();
$table->string('utype')->default('USR')->comment('ADM for Admin and USR for User or Customer');
$table->timestamps();
});
}


Now lets migrate the migration
So go to the command prompt and for migrating the migration run the command

php artisan migrate

now lets run the application
So just type the command

php artisan serve

Now switch to the project and just go inside the layouts directory
And from here just open blase.blade.php file and here add login and register link.
And also create the menu for admin and Normal user. For logout add the form. Write the following code in base.layout.php file

@if(Route::has('login'))
@auth
@if(Auth::user()->utype === 'ADM')
<li class=\"menu-item menu-item-has-children parent\" >
<a title=\"My Account\" href=\"#\">My Account ({{Auth::user()->name}})<i class=\"fa fa-angle-down\" aria-hidden=\"true\"></i></a>
<ul class=\"submenu curency\" >
<li class=\"menu-item\" >
<a title=\"Dashboard\" href=\"{{ route('admin.dashboard') }}\">Dashboard</a>
</li>
<li class=\"menu-item\">
<a href=\"{{ route('logout') }}\" onclick=\"event.preventDefault(); document.getElementById('logout-form').submit();\">Logout</a>
</li>
<form id=\"logout-form\" method=\"POST\" action=\"{{ route('logout') }}\">
@csrf
</form>
</ul>
</li>
@else
<li class=\"menu-item menu-item-has-children parent\" >
<a title=\"My Account\" href=\"#\">My Account ({{Auth::user()->name}})<i class=\"fa fa-angle-down\" aria-hidden=\"true\"></i></a>
<ul class=\"submenu curency\" >
<li class=\"menu-item\" >
<a title=\"Dashboard\" href=\"{{ route('user.dashboard') }}\">Dashboard</a>
</li>
<li class=\"menu-item\">
<a href=\"{{ route('logout') }}\" onclick=\"event.preventDefault(); document.getElementById('logout-form').submit();\">Logout</a>
</li>
<form id=\"logout-form\" method=\"POST\" action=\"{{ route('logout') }}\">
@csrf
</form>
</ul>
</li>
@endif
@else
<li class=\"menu-item\" ><a title=\"Register or Login\" href=\"{{route('login')}}\">Login</a></li>
<li class=\"menu-item\" ><a title=\"Register or Login\" href=\"{{route('register')}}\">Register</a></li>
@endif
@endif

Now go to the url http://localhost:8000/.
Now click on register link and lets create two user one for admin and another for normal user.
First of all create the admin user so in Name field enter Admin, in email field add email id of user like admin@surfsidemedia.in now eneter the password and confirm password.
And then click on Submit.
in Same way you can create another user which is Normarl User so in Name write User, in email id user@surfsidemedia.in, enter password and confirm password then click on submit.
Now we have created two users.
Now go to the http://localhost/phpmyadmin
And open the database then browse the users table.
And here you can see two user now change the user type of admin and just add here ADM.

Now lets create a middleware for Admin.
So go to the command prompt and for creating the middleware just type the command

php artisan make:middleware AuthAdmin

Now open the AuthAdmin middleware
Now just open the app\\http\\kernel.php file and inside the the routemiddleware array just add

'authadmin' => \\App\\Http\\Middleware\\AuthAdmin::class,


Now go inside the app\\Providers\\RouteServiceProvider.php
And here add the following

public const HOME = '/';

Now lets open the app\\http\\Middleware\\AuthAdmin.php file and write the following code.

<?php

namespace App\\Http\\Middleware;

use Closure;
use Illuminate\\Http\\Request;
use Illuminate\\Support\\Facades\\Auth;

class AuthAdmin
{
/**
* Handle an incoming request.
*
* @param \\Illuminate\\Http\\Request $request
* @param \\Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
if(Auth::user()->utype === 'ADM')
{
return $next($request);
}
else
{
session()->flush();
return redirect()->route('login');
}
return $next($request);
}
}

Now lets create two livewire component one for the Admin Dashboard and another for User Dashboard.
So go to command prompt and type following command

php artisan make:livewire Admin/AdminDashboardComponent
php artisan make:livewire User/UserDashboardComponent

Now switch to project and just go inside the app\\http\\livewire
And just open admin\\AdminDashboardComponent.php and user\\UserDashboardComponent.php class file and the layout.

class AdminDashboardComponent extends Component
{
public function render()
{
return view('livewire.admin.admin-dashboard-component')->layout('layouts.base');
}
}



class UserDashboardComponent extends Component
{
public function render()
{
return view('livewire.user.user-dashboard-component')->layout('layouts.base');
}
}


Now go to the resources\\views\\livewire and then open admin\\admin-dashboard-component.blade.php file add the following code.

<h1>Admin Dashboard</h1>

Now open the user\\user-dashboard-component.blade.php file add the following code.

<h1>User Dashboard</h1>

Now create the route for this component so just open web.php
And here lets create a route for the admin. So here I am going to create a route group for admin and also for the user.



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



//For User or Customer
Route::middleware(['auth:sanctum','verified'])->group(function(){
Route::get('/user/dashboard',UserDashboardComponent::class)->name('user.dashboard');
});

Now every thing set, so lets check it.
So switch to the browser and click on login
Now enter the user credential here and now you can see the user dashboard.
Now lets access the admin dashboard. So in url just add the admin
Now you can see we cannot access the admin dashboard, it redirect me on login page.
Now login with admin credentials and you can see the admin dashboard.
So in this way you can create admin and normal user authentication for the ecommerce project.