3

身份验证驱动程序/“多重身份验证”

在 laravel 5.2 发布之前,据说多重身份验证支持开箱即用。但是没有任何示例代码显示如何使用具有路由的不同驱动程序进行身份验证。所以我需要帮助设置使用默认 laravel 5.2 的多重身份验证

4

3 回答 3

6

创建两个新模型:App\AdminApp\User。更新config/auth.php

return [
    'defaults' => [
        'guard' => 'user',
        'passwords' => 'user',
    ],
    'guards' => [
        'user' => [
            'driver' => 'session',
            'provider' => 'user',
        ],
        'admin' => [
            'driver' => 'session',
            'provider' => 'admin',
        ],
    ],
    'providers' => [
        'user' => [
            'driver' => 'eloquent',
            'model' => 'App\User',
        ],
        'admin' => [
            'driver' => 'eloquent',
            'model' => 'App\Admin',
        ],
    ],
    'passwords' => [
        'user' => [
            'provider' => 'user',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ],
        'admin' => [
            'provider' => 'admin',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ]
    ]
];

在 kernel.php 中

    protected $middleware = [
        \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class
    ];

    /**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,

            //\App\Http\Middleware\VerifyCsrfToken::class,
        ],

        'api' => [
            'throttle:60,1',
        ],
    ];

并在 Route.php 中设置下面的代码和测试

    Route::get('/login', function() {
        $auth = auth()->guard('admin');

        $credentials = [
            'email' =>  'admin@gmail.com',
            'password' =>  'password',
        ];

        if ($auth->attempt($credentials)) {
            return redirect('/profile');
        } 
    });



    Route::get('/profile', function() {
            if(auth()->guard('admin')->check()){
                 print_r(auth()->guard('admin')->user()->toArray());
            } 

            if(auth()->guard('user')->check()){
                print_r(auth()->guard('user')->user()->toArray());
            }
        });
于 2015-12-22T12:49:27.090 回答
1

使用 rajpurohit-dinesh 示例,我们只需要完成第一步:

1:创建一个 App\Admin 模型(在我们的 app 文件夹上)。这是您的 Authenticatable 类的方式。

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;

Class Admin extends Authenticatable
{
//
}

2:更新config/auth.php。

return [
    // This is the default guard used, not need to declare
    // another guard here
    'defaults' => [
        'guard' => 'user',
            'passwords' => 'user',
    ],

    // Here we must to declare the guards, if we created the App\Admin
    // class as first step, we don't need to create a custom guard
    'guards' => [
        'user' => [
            'driver' => 'session',
            'provider' => 'user',
        ],
        'admin' => [
            'driver' => 'session',
            'provider' => 'admin',
        ],
    ],
    // In this example we are using only 'eloquent' driver
    'providers' => [
        'user' => [
            'driver' => 'eloquent',
            'model' => 'App\User',
        ],
        'admin' => [
            'driver' => 'eloquent',
            'model' => 'App\Admin',
        ],
    ],
    'passwords' => [
        'user' => [
            'provider' => 'user',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ],
        'admin' => [
            'provider' => 'admin',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ]
    ]
];

3:为了测试它,我们可以使用我们的 app\Http\Route.php 文件:

Route::get('/login', function() {
    $auth = auth()->guard('admin');

    $credentials = [
        'email' =>  'admin@gmail.com',
        'password' =>  'password',
    ];

    if ($auth->attempt($credentials)) {
        return 'Success';
    } else {
        return 'Not Success';    
});
于 2015-12-30T06:57:32.183 回答
0

感谢HoLiC的回答,它现在正在工作,但你可以尝试用Laravel带来的类来实现它吗?您只需添加路线:

Route::controller('/auth', 'Auth\AuthController');
Route::controller('/password', 'Auth\PasswordController');

并根据我上面的帖子在 resources/views/auth/login.blade.php 中创建表单。在此之后,您可以使用路线 laravel.dev/auth/login 和 laravel.dev/auth/logout

Starter auth 机制不能很好地工作,并且它与多 auth 不兼容。如果您检查,您可以通过 laravel.dev/auth/login 登录,但只有用户(无法在 AuthController 或任何使用管理员的地方进行设置)并且注销操作不起作用。如果你检查这个特性 Illuminate\Foundation\Auth\AuthenticatesUsers 你会发现这个机制现在是无用的,例如注销方法没有定义任何地方提供者管理员:

Auth::logout(); // not working logout should be smth like Auth::guard($provider)->logout();
于 2015-12-30T09:47:44.930 回答