To create custom multi-auth based on a field in users table then follow the steps:
Assuming you have installed Laravel and Authentication
Step 1: Add new row is_admin
in users table and model. then run the migration.
For detailed information for adding column to existing table :
http://blog.basaksecmen.com/laravel-add-a-column-or-columns-to-existing-table/
...
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->boolean('is_admin')->nullable();
});
}
...
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn(['is_admin']);
});
}
...
Then run the migration
php artisan migrate
Step 2: Edit Model:app/User.php
protected $fillable = [
'name', 'email', 'password', 'is_admin' //add here
];
Step 3: Create IsAdmin
Middleware to allow only admin
access users to that routes
php artisan make:middleware IsAdmin
Add this in IsAdmin
middleware: app/Http/Middleware/IsAdmin.php
public function handle($request, Closure $next)
{
if(auth()->user()->is_admin == 1){
return $next($request);
}
return redirect(‘home’)->with(‘error’,"You don't have admin access.");
}
Register your IsAdmin
middleware in app/Http/Kernel.php
protected $routeMiddleware = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
'is_admin' => \App\Http\Middleware\IsAdmin::class, // add this
];
Step 4: Create your route for admin in routes/web.php
Route::get('admin/home', 'HomeController@adminHome')->name('admin.home')->middleware('is_admin');
Step 5: Add adminHome()
method for admin route in app/Http/Controllers/HomeController.php
public function adminHome()
{
return view('adminHome');
}
Step 6: Create Blade file
resources/views/home.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">User Dashboard</div>
<div class="card-body">
@if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
@endif
User logged in!
</div>
</div>
</div>
</div>
</div>
@endsection
resources/views/adminHome.blade.php
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<div class="card">
<div class="card-header">Admin Dashboard</div>
<div class="card-body">
@if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
@endif
Admin logged in!
</div>
</div>
</div>
</div>
</div>
@endsection
Step 7: Change LoginController
, when user will login than we redirect according to user access. app/Http/Controllers/Auth/LoginController.php
...
use Illuminate\Http\Request;
...
public function login(Request $request)
{
$input = $request->all();
$this->validate($request, [
'email' => 'required|email',
'password' => 'required',
]);
if(auth()->attempt(array('email' => $input['email'], 'password' => $input['password'])))
{
if (auth()->user()->is_admin == 1) {
return redirect()->route('admin.home');
}else{
return redirect()->route('home');
}
}else{
return redirect()->route('login')
->with('error','Email-Address And Password Are Wrong.');
}
}
That’s all!