How to implement Multi Auth in Larvel 5.2. In this example, we use Two table admin and users Laravel 5.2 has a new artisan command.
Make a admin table as users table for simplicity.
Controller For Admin
Add two methods and specify $redirectTo and $guard
creating a middleware for admin
We can access authenticated user directly using
Auth::user() but if you have two authentication table then you have to use
php artisan make:authit will generate basic login/register route, view and controller for user table.
Make a admin table as users table for simplicity.
Controller For Admin
app/Http/Controllers/AdminAuth/AuthController(note: I just copied these files from app/Http/Controllers/Auth/AuthController here)
app/Http/Controllers/AdminAuth/PasswordController
config/auth.php
//Authenticating guardsroute.php
'guards' => [
'user' =>[
'driver' => 'session',
'provider' => 'user',
],
'admin' => [
'driver' => 'session',
'provider' => 'admin',
],
],
//User Providers
'providers' => [
'user' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admin' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
]
],
//Resetting Password
'passwords' => [
'clients' => [
'provider' => 'client',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
'admins' => [
'provider' => 'admin',
'email' => 'auth.emails.password',
'table' => 'password_resets',
'expire' => 60,
],
],
Route::group(['middleware' => ['web']], function () {AdminAuth/AuthController.php
//Login Routes...
Route::get('/admin/login','AdminAuth\AuthController@showLoginForm');
Route::post('/admin/login','AdminAuth\AuthController@login');
Route::get('/admin/logout','AdminAuth\AuthController@logout');
// Registration Routes...
Route::get('admin/register', 'AdminAuth\AuthController@showRegistrationForm');
Route::post('admin/register', 'AdminAuth\AuthController@register');
Route::get('/admin', 'AdminController@index');
});
Add two methods and specify $redirectTo and $guard
protected $redirectTo = '/admin';it will help you to open another login form for admin.
protected $guard = 'admin';
public function showLoginForm()
{
if (view()->exists('auth.authenticate')) {
return view('auth.authenticate');
}
return view('admin.auth.login');
}
public function showRegistrationForm()
{
return view('admin.auth.register');
}
creating a middleware for admin
class RedirectIfNotAdminregister middleware in kernel.php
{
/**
* 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('/');
}
return $next($request);
}
}
protected $routeMiddleware = [use this middleware in AdminController e.g.,
'admin' => \App\Http\Middleware\RedirectIfNotAdmin::class,
];
namespace App\Http\Controllers;That's all needed to make it working and also to get json of authenticated admin use
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
class AdminController extends Controller
{
public function __construct(){
$this->middleware('admin');
}
public function index(){
return view('admin.dashboard');
}
}
Auth::guard('admin')->user()Edit - 1
We can access authenticated user directly using
Auth::user() but if you have two authentication table then you have to use
Auth::guard('guard_name')->user()for logout
Auth::guard('guard_name')->user()->logout()for authenticated user json
Auth::guard('guard_name')->user()Source.
1 comment:
Casino É - FilmFileEurope
Casino É 스포츠토토 편의점 비코리아 놀검소 (filmfileeurope.eu) was established in 1992 by Steve Wynn. In 2018, they 파워 사다리 launched their own casino. They 메이져 사이트 began 토토 놀이터 넷마블 developing 합법 토토 사이트 넷마블 their own casino.
Post a Comment