简体   繁体   English

如何在laravel中为auth控制器设置新的防护

[英]how to set new guard for auth controller in laravel

I want to make multiple authentication in my laravel project. 我想在我的laravel项目中进行多重身份验证。

I create new guard "admin" in my auth.php file but I don't know how to set new created guard in my authcontroller. 我在我的auth.php文件中创建了新的后卫“admin”,但我不知道如何在我的authcontroller中设置新创建的后卫。

It always use "defaults" settings from my auth.php: 它始终使用我的auth.php中的“默认值”设置:

'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],

1) If you wish to use newly created admin guard throughout the application, you can change the value in defaults of config file. 1)如果您希望在整个应用程序中使用新创建的admin防护,您可以更改配置文件的默认值


2) If it is only about AuthController that uses Laravel's in built Auth system, you can add this line in the AuthController.php and PasswordController.php : 2)如果只是在AuthController中使用Laravel的内置Auth系统,你可以在AuthController.phpPasswordController.php中添加这一行:

protected $guard = 'admin';

Ref - Check Guard Customization here 参考 - 在此检查Guard Customization


3) If you wish to you a guard other than default one for any Auth related task, you can specify it manually like this : 3)如果您希望为任何与Auth相关的任务执行非默认保护,您可以手动指定它,如下所示:

// For route middleware
Route::get('profile', [
    'middleware' => 'auth:admin',
    'uses' => 'ProfileController@show'
]);

// For manually logging the user in
if (Auth::guard('admin')->attempt($credentials)) {
    // Authenticated...
}

// To login specific user using eloquent model
Auth::guard('admin')->login($user);

// For getting logged in user
Auth::guard('admin')->user();

// To check if user is logged in
if (Auth::guard('admin')->check()) {
    // Logged in
}

Ref - https://laravel.com/docs/5.2/authentication 参考 - https://laravel.com/docs/5.2/authentication

Laravel 5.4+ uses the following in any Auth Controller: Laravel 5.4+在任何Auth Controller中使用以下内容:

use Illuminate\Support\Facades\Auth;

protected function guard(){
   return Auth::guard('guard-name');
}

You have to also register the guard in the config\\auth.php 您还必须在config\\auth.php注册guard

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],

     'admin' => [
        'driver' => 'session',
        'provider' => 'admins',
    ],
],

admin is the guard name, provider is the TABLE-NAME admin是保护名称,provider是TABLE-NAME

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM