如何在Laravel 5.6中创建多重身份验证? [英] How to create multi auth in laravel 5.6?
问题描述
我曾经使用过laravel 5.5,并且早于 https://github.com/Hesto/multi- auth .
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,所以我在写我自己的问题的Answer.
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
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.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
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 \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
中使用此中间件
例如,
use this middleware in AdminController
e.g.,
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()
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屋!