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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49315781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:31:18  来源:igfitidea点击:

How to create multi auth in laravel 5.6?

laravellaravel-5

提问by sathish R

I used to be for laravel 5.5 and earlier than https://github.com/Hesto/multi-auth.

我曾经为 laravel 5.5 及早于https://github.com/Hesto/multi-auth

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 中创建多重身份验证?

回答by mySun

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.

经过大量的挖掘和大量的问题和答案,我终于设法使用两个表来使用 Laravel 5.6 Multi Auth,所以我正在写我自己的问题的答案。

How to implement Multi Auth in Larvel

如何在 Larvel 中实现多重身份验证

As Mentioned above. Two table adminand users

正如刚才提到的。两张桌子adminusers

Laravel 5.2 has a new artisancommand.

Laravel 5.2 有一个新artisan命令。

php artisan make:auth

php artisan make:auth

it will generate basic login/register route, viewand controllerfor usertable.

它会产生基本的登录/注册routeviewcontrolleruser表。

Make a admintable as userstable for simplicity.

为简单起见,制作一张admin桌子作为users桌子。

Controller For Admin
app/Http/Controllers/AdminAuth/AuthController
app/Http/Controllers/AdminAuth/PasswordController
(note: I just copied these files from app/Http/Controllers/Auth/AuthControllerhere)

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

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

Add two methods and specify $redirectToand $guard

添加两个方法并指定$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');
}  

it will help you to open another login form for admin

它将帮助您为管理员打开另一个登录表单

creating a middleware for admin

创建一个中间件 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);
}

}

}

register middleware in kernel.php

注册中间件 kernel.php

 protected $routeMiddleware = [
    'admin' => \App\Http\Middleware\RedirectIfNotAdmin::class,
];

use this middleware in AdminControllere.g.,

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

That's all needed to make it working and also to get json of authenticated admin use
Auth::guard('admin')->user()

这是使其正常工作以及获取经过身份验证的管理员使用的 json 所需的全部内容
Auth::guard('admin')->user()

We can access authenticated user directly using
Auth::user()but if you have two authentication table then you have to use

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

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

for logout

登出

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

for authenticated user json

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

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