任何人都可以通过示例解释Laravel 5.2多重身份验证 [英] Can Anyone Explain Laravel 5.2 Multi Auth with Example
问题描述
我正在尝试分别通过user
表和admin
表对用户和 admin 进行身份验证.我正在使用laravel提供的User
模型,并为Admin.
创建了相同的模型.我在auth.php.
I am trying to authenticate users and admin form user
table and admin
table respectively. I am using the User
model as provided by laravel out of the box and created the same for Admin.
I have added a guard key and provider key into auth.php.
卫兵
'guards' => [
'user' =>[
'driver' => 'session',
'provider' => 'user',
],
'admin' => [
'driver' => 'session',
'provider' => 'admin',
],
],
提供商
'providers' => [
'user' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admin' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
]
],
路线
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');
});
我创建了一个名为AuthAdmin
的目录,其中存在Laravel的默认AuthController.php
和PasswordController.php
文件. (命名空间已相应修改)
I have created a directory called AuthAdmin
where Laravel's default AuthController.php
and PasswordController.php
files are present. (Namespace Modified accordingly)
首先,在Laravel的文档中提到了如何在进行身份验证时指定自定义防护,这是行不通的.
First of all, in Laravel's docs mentioned that how to specify custom guard while authenticating like this which isn't working.
Laravel的文档中提到了另一种方法,即使用一种也不起作用的防护装置.
There's another method mentioned in Laravel's docs to use a guard which is not working too.
如果有人解决我的问题并纠正我的话,那将是有益的.
It would be beneficial if someone could resolve the issues and correct me if I am wrong.
推荐答案
经过大量的挖掘和大量的问题&我终于设法在两个表上使用Laravel 5.2 Multi Auth,所以我在写我自己的问题的Answer.
After lots of digging and lots of questions & answers I have finally managed to work Laravel 5.2 Multi Auth with two table, So I'm writing Answer of my own Question.
如何在Larvel 5.2中实现多重身份验证
如上所述.
两个表admin
和users
As Mentioned above.
Two table admin
and users
Laravel 5.2具有新的artisan
命令.
Laravel 5.2 has a new artisan
command.
php artisan make:auth
它将为user
表生成基本的登录/注册route
,view
和controller
.
it will generate basic login/register route
, view
and controller
for user
table.
为简单起见,将admin
表作为users
表.
Make a admin
table as users
table for simplicity.
管理员控制器
app/Http/Controllers/AdminAuth/AuthController
app/Http/Controllers/AdminAuth/PasswordController
(注意:我只是从app/Http/Controllers/Auth/AuthController
这里复制了这些文件)
Controller For Admin
app/Http/Controllers/AdminAuth/AuthController
app/Http/Controllers/AdminAuth/PasswordController
(note: I just copied these files from app/Http/Controllers/Auth/AuthController
here)
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
添加两个方法并指定$redirectTo
和$guard
Add two methods and specify $redirectTo
and $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
为admin
creating a middleware for 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);
}
}
在kernel.php
protected $routeMiddleware = [
'admin' => \App\Http\Middleware\RedirectIfNotAdmin::class,
];
在AdminController
中使用此中间件
例如,
use this middleware in AdminController
e.g.,
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');
}
}
这是使它正常工作以及获得经过身份验证的管理员使用的json的所有必要条件
Auth::guard('admin')->user()
That's all needed to make it working and also to get json of authenticated admin use
Auth::guard('admin')->user()
编辑-1
我们可以使用
直接访问经过身份验证的用户
Auth::user()
但是,如果您有两个身份验证表,则必须使用
Edit - 1
We can access authenticated user directly using
Auth::user()
but if you have two authentication table then you have to use
Auth::guard('guard_name')->user()
注销
Auth::guard('guard_name')->user()->logout()
用于经过身份验证的用户json
for authenticated user json
Auth::guard('guard_name')->user()
编辑2
现在您可以下载Laravel 5.2 Multiauth实现的项目 http://imrealashu .in/code/laravel/multi-auth-with-laravel-5-2-2/
这篇关于任何人都可以通过示例解释Laravel 5.2多重身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!