Laravel 5.2 需要一个实现默认身份验证驱动程序/“Multi-Auth”的示例。现在需要很多工作,因为[重复]
Posted
技术标签:
【中文标题】Laravel 5.2 需要一个实现默认身份验证驱动程序/“Multi-Auth”的示例。现在需要很多工作,因为[重复]【英文标题】:Laravel 5.2 need an example which implements default Authentication Drivers / "Multi-Auth". which needs lots of works right now as [duplicate] 【发布时间】:2016-03-28 14:27:12 【问题描述】:身份验证驱动程序/“多重身份验证”
在 laravel 5.2 发布之前,它声明多重身份验证支持开箱即用。但是没有任何示例代码显示如何使用具有路由的不同驱动程序进行身份验证。所以我需要帮助使用默认 laravel 5.2 设置多重身份验证
【问题讨论】:
Laravel 5.2 Multi Auth 解决方案在这里。请查看此链接:***.com/questions/34490600/… 这是解决方案***.com/questions/34614753/… ***.com/questions/34614753/… 另一个例子***.com/questions/34490600/… 【参考方案1】:创建两个新模型: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,
]
]
];
在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());
);
【讨论】:
看起来不错,但似乎有很多重复(guards
、providers
、passwords
真的很相似...)【参考方案2】:
使用 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';
);
【讨论】:
【参考方案3】:感谢 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 身份验证机制无法正常工作,并且与多重身份验证不兼容。如果您检查,您可以通过 laravel.dev/auth/login 登录,但只有用户(无法在 AuthController 或任何使用管理员的地方进行设置)并且注销操作不起作用。如果你检查这个特性 Illuminate\Foundation\Auth\AuthenticatesUsers 你会发现这个机制现在没有用,例如注销方法没有定义任何地方提供者管理员:
Auth::logout(); // not working logout should be smth like Auth::guard($provider)->logout();
【讨论】:
以上是关于Laravel 5.2 需要一个实现默认身份验证驱动程序/“Multi-Auth”的示例。现在需要很多工作,因为[重复]的主要内容,如果未能解决你的问题,请参考以下文章
在AuthController中覆盖laravel的5.2身份验证方法
我正在尝试为 user 和 admin 实现 laravel 5.2 多重身份验证。但是未定义身份验证用户提供程序 [] 错误给出