如何在laravel 5.6中创建多个auth?

时间:2018-03-16 08:06:01

标签: laravel laravel-5

我以前是为了laravel 5.5而且早于https://github.com/Hesto/multi-auth

但是这个存储库不会为laravel 5.6更新。

如何在Laravel 5.6中创建多个身份验证?

1 个答案:

答案 0 :(得分:21)

经过大量挖掘和大量问题后,答案我终于设法用两张桌子来工作Laravel 5.6 Multi Auth,所以我写了我自己的问题的答案。

如何在Larvel中实现Multi Auth

如上所述。 两个表adminusers

Laravel 5.2有一个新的artisan命令。

php artisan make:auth

它将为route表格生成基本登录/注册viewcontrolleruser

为简单起见,将admin表格设为users表。

管理员控制器
app/Http/Controllers/AdminAuth/AuthController
app/Http/Controllers/AdminAuth/PasswordController
(注意:我刚从app/Http/Controllers/Auth/AuthController这里复制了这些文件)

config/auth.php

//Authenticating guards
'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.php

Route::group(['middleware' => ['web']], function () {
    //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');

});  

AdminAuth/AuthController.php

添加两种方法并指定$redirectTo$guard

protected $redirectTo = '/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');
}  

它可以帮助您打开管理员的另一个登录表单

admin

创建中间件
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('/');
    }

    return $next($request);
}

}

kernel.php

中注册中间件
 protected $routeMiddleware = [
    'admin' => \App\Http\Middleware\RedirectIfNotAdmin::class,
];

AdminController中使用此中间件 例如,

namespace App\Http\Controllers;

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');
    }
}

所有这些都需要使其工作,并且还要获得经过身份验证的管理员使用的json  Auth::guard('admin')->user()

我们可以使用
直接访问经过身份验证的用户 Auth::user() 但如果您有两个身份验证表,则必须使用

Auth::guard('guard_name')->user()  

用于注销

Auth::guard('guard_name')->user()->logout()

用于经过身份验证的用户json

Auth::guard('guard_name')->user()