In this video we are going to learn about Creating Admin and User Authentication
For authentication here I am going to use laravel/ui Package
I have already installed the laravel/ui package in previous video
Now lets customize this
First of all open the user migration
And here add a column
Now lets refresh the migration
Run the command in command prompt
Now lets create a middleware for the admin
So run the command
Now open the This AuthAdmin middleware from App\Http\Middleware
And inside the handle method add the following
Now go to the App\Http and from here lets open Kernel.php file
Inside the middleware Aliases add here
Now go to the App\Providers directory and from here open RouteServiceProvider.php file
And here from this const Home variable just remove this home only add here forward slash (/)
Now lets create two controller one for the user and another for the admin
So run the command in command prompt
Ok controllers are created now lets open these controller
So go to the controller directory and from here lets open the User controller
And add a function inside this controller
Now open AdminController and add a function
Now lets create this index view
So in resources directory
Go inside the views folder and lets open the views directory
And here lets create one for user an and another for the admin
Now inside these folder create index.blade.php file
In this view file add the layout tag here
Similarly in admin index view add here
Now lets create route for these
Go to the web.php file
And here add the route for the user and admin
Now go the layout file And here add the menu
Now all done so lets check
So run the application
Now go to the Localhost:8000
Here is the login and register link
Now lets register two user one for the admin and another for normal user
Admin
admin@surfsidemedia.in
12345678
Now user created lets logout and register another user
User
user@surfsidemedia.in
12345678
Now go to the phpMyAdmin
And lets open the database
And browse the users table
Now edit this record and change the utype as ADM for the admin user
Now lets check this so logout from here
Now lets login with admin credential
Here is the admin dashboard
Now if I login with user credentials you can see the user dashboard is here
If I want access admin page it will redirect us on login page
So in this way you can create Admin and User Authentication for the Ecommerce Project
so that's all about Creating admin and user authentication.
For authentication here I am going to use laravel/ui Package
I have already installed the laravel/ui package in previous video
Now lets customize this
First of all open the user migration
And here add a column
$table->string('utype')->default('USR')->comment('ADM for Admin and USR for User or Customer');
Now lets refresh the migration
Run the command in command prompt
php artisan migrate:refresh
Now lets create a middleware for the admin
So run the command
php artisan make:middleware AuthAdmin
Now open the This AuthAdmin middleware from App\Http\Middleware
And inside the handle method add the following
if(Auth::user()->utype != 'ADM')
{
session()->flush();
return redirect()->route('login');
}
Now go to the App\Http and from here lets open Kernel.php file
Inside the middleware Aliases add here
'auth.admin' => \App\Http\Middleware\AuthAdmin::class,
Now go to the App\Providers directory and from here open RouteServiceProvider.php file
And here from this const Home variable just remove this home only add here forward slash (/)
public const HOME = '/';
Now lets create two controller one for the user and another for the admin
So run the command in command prompt
php artisan make:controller UserController
Php artisan make:controller AdminController
Ok controllers are created now lets open these controller
So go to the controller directory and from here lets open the User controller
And add a function inside this controller
public function index()
{
return view("users.index");
}
Now open AdminController and add a function
public function index()
{
return view("admin.index");
}
Now lets create this index view
So in resources directory
Go inside the views folder and lets open the views directory
And here lets create one for user an and another for the admin
Now inside these folder create index.blade.php file
In this view file add the layout tag here
@extends('layouts.base')
@section('content')
<h1>User Dashboard</h1>
@endsection
Similarly in admin index view add here
@extends('layouts.base')
@section('content')
<h1>Admin Dashboard</h1>
@endsection
Now lets create route for these
Go to the web.php file
And here add the route for the user and admin
Route::middleware('auth')->group(function(){
Route::get('/my-account',[UserController::class,'index'])->name('user.index');
});
Route::middleware(['auth','auth.admin'])->group(function(){
Route::get('/admin',[AdminController::class,'index'])->name('admin.index');
});
Now go the layout file And here add the menu
<li class="onhover-dropdown">
<div class="cart-media name-usr">
@auth <span>{{ Auth::user()->name }}</span> @endauth <i data-feather="user"></i>
</div>
<div class="onhover-div profile-dropdown">
<ul>
@if(Route::has('login'))
@auth
@if(Auth::user()->utype === 'ADM')
<li>
<a href="{{route('admin.index')}}" class="d-block">Dashboard</a>
</li>
@else
<li>
<a href="{{route('user.index')}}" class="d-block">My Account</a>
</li>
@endif
<li>
<a href="{{route('logout')}}" onclick="event.preventDefault();document.getElementById('frmlogout').submit();" class="d-block">Logout</a>
<form id="frmlogout" action="{{route('logout')}}" method="POST">
@csrf
</form>
</li>
@else
<li>
<a href="{{route('login')}}" class="d-block">Login</a>
</li>
<li>
<a href="{{route('register')}}" class="d-block">Register</a>
</li>
@endauth
@endif
</ul>
</div>
</li>
Now all done so lets check
So run the application
Php artisan serve
Now go to the Localhost:8000
Here is the login and register link
Now lets register two user one for the admin and another for normal user
Admin
admin@surfsidemedia.in
12345678
Now user created lets logout and register another user
User
user@surfsidemedia.in
12345678
Now go to the phpMyAdmin
And lets open the database
And browse the users table
Now edit this record and change the utype as ADM for the admin user
Now lets check this so logout from here
Now lets login with admin credential
Here is the admin dashboard
Now if I login with user credentials you can see the user dashboard is here
If I want access admin page it will redirect us on login page
So in this way you can create Admin and User Authentication for the Ecommerce Project
so that's all about Creating admin and user authentication.