Solution :
Step1: Now open config/auth.php and make the following changes.
Step2 :
‘guards’ => [
‘web’ => [
‘driver’ => ‘session’,
‘provider’ => ‘users’,
],
‘admin’ => [
‘driver’ => ‘session’,
‘provider’ => ‘admin’,
],
],
‘providers’ => [
‘users’ => [
‘driver’ => ‘eloquent’,
‘model’ => App\User::class,
],
‘admin’ => [
‘driver’ => ‘eloquent’,
‘model’ => App\Admin::class,
],
],
‘passwords’ => [
‘users’ => [
‘provider’ => ‘users’,
’email’ => ‘auth.emails.password’,
‘table’ => ‘password_resets’,
‘expire’ => 60,
],
‘admin’ => [
‘provider’ => ‘admin’,
’email’ => ‘auth.emails.password’,
‘table’ => ‘password_resets’,
‘expire’ => 60,
],
],
Step3: Create a new Middleware RedirectIfNotAdmin
step4 : Now open RedirectIfNotAdmin file and add following code
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class RedirectIfNotAdmin
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = ‘admin’)
{
if (!Auth::guard($guard)->check()) {
return redirect(‘admin/login’);
}
return $next($request);
}
}
step5: open Kernel.php // path : app/Http/kernal.php
Step6: Add following line at
protected $routeMiddleware = [
‘admin’ => \App\Http\Middleware\RedirectIfNotAdmin::class,
];
Step7 : To check login use following in your controller
public function __construct(){
$this->middleware(‘admin’);
}
Step8: Create custom Login controller
namespace App\Http\Controllers;
use Auth, Input;
use App\User;
use App\Admin;
class LoginController extends Controller
{
public function adminLogin(){
$input = Input::all();
if(count($input) > 0){
$auth = auth()->guard(‘admin’);
$credentials = [
’email’ => $input[’email’],
‘password’ => $input[‘password’],
];
if ($auth->attempt($credentials)) {
return redirect()->action(‘[email protected]’);
} else {
echo ‘Error’;
}
} else {
return view(‘admin.login’);
}
}
}
Step9: Now your custom admin is working. default is Auth::attempt() and it will always check from user table. but if you want to check Auth from Admin table then use above procedure and to check user is login or not use
Auth::guard(‘admin’)->check() // it will return true or false, in stead Auth::check();
For more info mail us : [email protected]
Thanks 🙂