Laravel 5.2中具有单用户表的多身份验证

时间:2019-02-13 07:36:52

标签: php laravel laravel-5

我想在laravel 5.2中使用single表进行多重身份验证。

我尝试过这种方式。但是前端登录有效。

Laravel 5.2具有新的artisan命令。

php artisan make:auth

它将为route表生成基本的登录/注册viewcontrolleruser

为简单起见,将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');
    }
}

2 个答案:

答案 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)

你不需要在这里使用守卫,而是;你可以在这里使用中间件。在用户表中添加一个用户类型列,然后为此创建中间件。