我想在laravel 5.2中使用single
表进行多重身份验证。
我尝试过这种方式。但是前端登录有效。
Laravel 5.2具有新的artisan
命令。
php artisan make:auth
它将为route
表生成基本的登录/注册view
,controller
和user
。
为简单起见,将admin
表作为users
表。
管理员控制器
app/Http/Controllers/AdminAuth/AuthController
app/Http/Controllers/AdminAuth/PasswordController
(注意:我只是从app/Http/Controllers/Auth/AuthController
复制了这些文件)
config/auth.php
//Authenticating guards
return [
'guards' => [
'user' =>[
'driver' => 'session',
'provider' => 'user',
],
'admin' => [
'driver' => 'session',
'provider' => 'admin',
],
],
//User Providers
'providers' => [
'user' => [
'driver' => 'database',
'table' => 'user',
'model' => App\User::class,
],
'admin' => [
'driver' => 'database',
'table' => 'user',
'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::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
添加两个方法并指定$redirectTo
和$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');
}
它将帮助您为管理员打开另一个登录表单
为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
中使用此中间件
例如,
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');
}
}
答案 0 :(得分:0)
在您的AdminAuth/AuthController.php
中。
public function guard()
{
return auth()->guard('admin');
}
,而不是在提供程序内部提供表名,而是将protected $table = 'users'
放入您的Admin模型中。
您可以查看此以获取更多详细信息:https://scotch.io/@sukelali/how-to-create-multi-table-authentication-in-laravel
答案 1 :(得分:-1)
你不需要在这里使用守卫,而是;你可以在这里使用中间件。在用户表中添加一个用户类型列,然后为此创建中间件。