Laravel 5.2 需要一个实现默认身份验证驱动程序/“多重身份验证”的示例。现在需要做很多工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34413887/
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
Laravel 5.2 need an example which implements default Authentication Drivers / "Multi-Auth". which needs lots of works right now as
提问by msonowal
Authentication Drivers / "Multi-Auth"
身份验证驱动程序/“多重身份验证”
as prior to release of laravel 5.2 it is stated that multi auth suppots out of the box. but there is no any example codes showing how to authenticate using different drivers with routes. So I need help setting up the multi-auth using default laravel 5.2
在 laravel 5.2 发布之前,据说多重身份验证支持开箱即用。但是没有任何示例代码显示如何使用不同的驱动程序进行身份验证和路由。所以我需要帮助使用默认的 laravel 5.2 设置多重身份验证
回答by DsRaj
Create two new models: App\Admin
and App\User
. Update config/auth.php
:
创建两个新模型:App\Admin
和App\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,
]
]
];
In kernel.php
在内核.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',
],
];
and in Route.php set below code and test
并在 Route.php 设置下面的代码和测试
Route::get('/login', function() {
$auth = auth()->guard('admin');
$credentials = [
'email' => '[email protected]',
'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());
}
});
回答by HoLiC
Using the rajpurohit-dinesh example, we just need to finnish first step:
使用 rajpurohit-dinesh 示例,我们只需要完成第一步:
1: Create an App\Admin model (on our app folder). Here is how should be your Authenticatable class.
1:创建一个 App\Admin 模型(在我们的 app 文件夹中)。这是您的 Authenticatable 类应该如何。
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
Class Admin extends Authenticatable
{
//
}
2: Update config/auth.php.
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: To test it, we can use our app\Http\Route.php file:
3:为了测试它,我们可以使用我们的 app\Http\Route.php 文件:
Route::get('/login', function() {
$auth = auth()->guard('admin');
$credentials = [
'email' => '[email protected]',
'password' => 'password',
];
if ($auth->attempt($credentials)) {
return 'Success';
} else {
return 'Not Success';
});
回答by bartw2k9
Thanks for answer HoLiC, it's working right now but can you try to implement it with classes what Laravel's bring on start? You just have to add routes:
感谢您的回答 HoLiC,它现在正在工作,但是您可以尝试使用 Laravel 开始时带来的类来实现它吗?你只需要添加路由:
Route::controller('/auth', 'Auth\AuthController');
Route::controller('/password', 'Auth\PasswordController');
and create form in resources/views/auth/login.blade.php from my post above. After this you can use routes laravel.dev/auth/login and laravel.dev/auth/logout
并从我上面的帖子中在 resources/views/auth/login.blade.php 中创建表单。在此之后,您可以使用路由 laravel.dev/auth/login 和 laravel.dev/auth/logout
Starter auth mechanism doesn't working good and its not compatibile with multi auth. If you check you can login via laravel.dev/auth/login but only user(theres no any way to setup in AuthController or anyplace to use admins) and logout action not working. If you check this trait Illuminate\Foundation\Auth\AuthenticatesUsers you will see that this mechanims is unuseful right now for example logout method not defines anywhere provider admin:
Starter auth 机制不能很好地工作,并且它与多重身份验证不兼容。如果你检查你可以通过 laravel.dev/auth/login 登录,但只有用户(没有任何方法可以在 AuthController 或任何地方使用管理员进行设置)并且注销操作不起作用。如果你检查这个 trait Illuminate\Foundation\Auth\AuthenticatesUsers 你会发现这个机制现在没有用,例如注销方法没有在任何地方定义提供者管理员:
Auth::logout(); // not working logout should be smth like Auth::guard($provider)->logout();