如何在 Laravel 5.6 中创建多重身份验证? [英] How to create multi auth in laravel 5.6?
问题描述
我曾经为 laravel 5.5 及早于 https://github.com/Hesto/multi-授权 .
I used to be for laravel 5.5 and earlier than https://github.com/Hesto/multi-auth .
但是此存储库不会针对 laravel 5.6 进行更新.
But this repository don't update for laravel 5.6.
如何在 Laravel 5.6 中创建多重身份验证?
How to create multi auth in Laravel 5.6?
推荐答案
经过大量挖掘和大量问题 &我终于设法使用两个表使用 Laravel 5.6 Multi Auth,所以我正在写我自己的问题的答案.
After lots of digging and lots of questions & answers I have finally managed to work Laravel 5.6 Multi Auth with two table, So I'm writing Answer of my own Question.
如何在Larvel中实现多重身份验证
如上所述.两个表 admin
和 users
As Mentioned above.
Two table admin
and users
Laravel 5.2 有一个新的 artisan
命令.
Laravel 5.2 has a new artisan
command.
php artisan make:auth
它将为user
表生成基本的登录/注册route
、view
和controller
.
it will generate basic login/register route
, view
and controller
for user
table.
为简单起见,将 admin
表设为 users
表.
Make a admin
table as users
table for simplicity.
管理员控制器app/Http/Controllers/AdminAuth/AuthController
app/Http/Controllers/AdminAuth/PasswordController
(注意:我只是从 app/Http/Controllers/Auth/AuthController
这里复制了这些文件)
Controller For Admin
app/Http/Controllers/AdminAuth/AuthController
app/Http/Controllers/AdminAuth/PasswordController
(note: I just copied these files from app/Http/Controllers/Auth/AuthController
here)
config/auth.php
//Authenticating guards
'guards' => [
'user' =>[
'driver' => 'session',
'provider' => 'user',
],
'admin' => [
'driver' => 'session',
'provider' => 'admin',
],
],
//User Providers
'providers' => [
'user' => [
'driver' => 'eloquent',
'model' => AppUser::class,
],
'admin' => [
'driver' => 'eloquent',
'model' => AppAdmin::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','AdminAuthAuthController@showLoginForm');
Route::post('/admin/login','AdminAuthAuthController@login');
Route::get('/admin/logout','AdminAuthAuthController@logout');
// Registration Routes...
Route::get('admin/register', 'AdminAuthAuthController@showRegistrationForm');
Route::post('admin/register', 'AdminAuthAuthController@register');
Route::get('/admin', 'AdminController@index');
});
AdminAuth/AuthController.php
添加两个方法并指定$redirectTo
和$guard
Add two methods and specify $redirectTo
and $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');
}
它将帮助您为管理员打开另一个登录表单
it will help you to open another login form for admin
为admin
创建一个中间件
creating a middleware for admin
class RedirectIfNotAdmin
{
/**
* Handle an incoming request.
*
* @param IlluminateHttpRequest $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' => AppHttpMiddlewareRedirectIfNotAdmin::class,
];
在AdminController
中使用这个中间件例如,
use this middleware in AdminController
e.g.,
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppHttpRequests;
use AppHttpControllersController;
use IlluminateSupportFacadesAuth;
class AdminController extends Controller
{
public function __construct(){
$this->middleware('admin');
}
public function index(){
return view('admin.dashboard');
}
}
这一切都需要使其正常工作并获得经过身份验证的管理员使用的 jsonAuth::guard('admin')->user()
That's all needed to make it working and also to get json of authenticated admin use
Auth::guard('admin')->user()
我们可以使用
直接访问经过身份验证的用户Auth::user()
但是如果你有两个身份验证表,那么你必须使用
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()
用于注销
Auth::guard('guard_name')->user()->logout()
对于经过身份验证的用户 json
for authenticated user json
Auth::guard('guard_name')->user()
这篇关于如何在 Laravel 5.6 中创建多重身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!